How to access child class object that inherits parent class?

我是研究僧i 提交于 2021-01-29 09:24:15

问题


I have parent class and child class, that inherits parent class. And that is okay, I can iterate with for loop. Now I want to access child class (example: 'product_type' So basically, I'm confused how we inherits stuff from child class inside the same loop...

views.py

from django.views import generic
from . models import Category
from django.shortcuts import render

class CategoryListView(generic.ListView):
    model = Category
    template_name = 'category_list.html'

models.py

from django.db import models
import uuid
    
class Category(models.Model):
    name = models.CharField(max_length=100, help_text='Category name')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Categories'
    
class Product(models.Model):
    product_name = models.CharField(max_length=255, help_text='Product name')
    # product_spec = models.TextField(max_length=5000, help_text='Product specs')
    product_type = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.product_name

category_list.html

{% extends 'base.html' %}


{% block body %}

{% for page in category_list %}
    <li>{{ page.name }}</li>
    <li>{{ page.product_name }} # <--------------- Now this is the point of 
                                                   #my problem, I want to get 
                                                   #product name from child 
                                                   #class 
                                                   #this returns empty <li>
{% endfor %}

{% endblock %}

回答1:


you can to this

{% extends 'base.html' %}


{% block body %}

{% for page in category_list %}
    <li>{{ page.name }}</li>
    <li>{{ page.product_set.first.product_name }} 
product name from child class, this returns empty <li>
{% endfor %}

{% endblock %}



回答2:


First off change your product_type name to just category its way easier to understand and add an attribute related_name to it like this:

class Product(models.Model):
    name = models.CharField(max_length=255, help_text='Product name')
    category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True, related_name='products')

then in your template

{% for category in category_list %}
    {{ category.name }}
    {% for product in category.products.all %}
        {{ product.name}}
        ... other product data
    {% endfor %}
{% endfior %}
      



回答3:


product_typein Product is a ForaignKey which means their will be multiple products having same Category so their exist two solutions

  1. make product_type in Product one to one key, with this you shuld get single name by {{ page.product.product_name }}

  2. print list of all products of the category, you can do this by iterating page.product_set as it is a list (iterable) with for loop.



来源:https://stackoverflow.com/questions/65685204/how-to-access-child-class-object-that-inherits-parent-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!