【问题标题】:Django: List Products of each Categories in a pageDjango:列出页面中每个类别的产品
【发布时间】:2018-10-01 13:08:09
【问题描述】:

我有几个类别,我想按以下格式列出每个类别的产品(类别是产品的 FK):

第 1 类

一堆产品

....

类别 N

一堆产品

我尝试了很多方法,但到目前为止,我只获得了要在我的 HTML 中显示的类别而不是产品。

模型.py

class Category(models.Model):
    title = models.CharField(max_length=225)
    slug = models.SlugField(unique=True, blank=True, null=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('category_detail', kwargs={'slug': self.slug})

    @property
    def get_products(self):
         return Products.objects.filter(category=self.title)

class Products(models.Model):
    title = models.CharField(max_length=225)
    image = models.ImageField(upload_to=upload_image_path, null=True, blank=True)
 blank=True, on_delete=models.CASCADE)
    categories = models.ForeignKey(Category, related_name='Category', blank=True, on_delete=models.CASCADE) #this
    gender = models.CharField(max_length=20, choices=GENDER_CHOICES, default="male")

    objects = ProductManager()

    def get_absolute_url(self):
        return reverse('product_detail', args=(self.id,))

    def __str__(self):
        return self.title

views.py

def categories_m(request):
    query_Set = models.Category.objects.all()
    page = request.GET.get('page', 1)
    paginator = Paginator(query_Set, 20)
    try:
        cat = paginator.page(page)
    except PageNotAnInteger:
        cat = paginator.page(1)
    except EmptyPage:
        cat = paginator.page(paginator.num_pages)
    return render(request, 'products/categories.html', {'categories': cat})

html

{% extends 'base.html' %}

{% block content %}
{% for category in categories %}
    <h1>{{ category }}</h1>
    {% for product in categories.get_products %}
        <p>{{ product }}</p>
    {% endfor %}
{% endfor %}
{% endblock %}

【问题讨论】:

    标签: python django django-models django-templates django-views


    【解决方案1】:

    在您的模板中应该是 category 而不是 categories。并在您的模型中按类别标题过滤产品。

    @property
    def get_products(self):
         return Products.objects.filter(categories__title=self.title)
    


    {% extends 'base.html' %}
    
        {% block content %}
            {% for category in categories %}
            <h1> {{category}} </h1>
    
            {% for product in category.get_products %}
                <p> {{product}} </p>
            {% endfor %}
    
            {% endfor %}
        {% endblock %}
    

    【讨论】:

    • 不,这不起作用。我以前试过这个。它抛出:无法将关键字“类别”解析为字段。选项有:活动、类别、categories_id、性别、id、图像、标题 ....
    • @Inconnu 在您的模型中删除 @property 装饰器。
    • 你尝试导入Category 并设置query_Set = Category.objects.all() 吗?我不确定这是什么原因,只是说。
    【解决方案2】:

    您可以从您的字段中删除 related_name 属性

    class Products(models.Model):
        categories = models.ForeignKey(Category, blank=True, on_delete=models.CASCADE)
    

    然后在你的模板中你可以使用category.products_set.all

    {% extends 'base.html' %}
    
        {% block content %}
        {% for category in categories %}
            <h1>{{ category }}</h1>
            {% for product in category.products_set.all %}
                <p>{{ product }}</p>
            {% endfor %}
        {% endfor %}
        {% endblock %}
    

    【讨论】:

    • 他为什么要删除related_name ??他可以使用category.Category.all
    • 嗯...我的措辞可能很糟糕:我的意思是定义related_name(或使用默认值)在语义上更正确,这样您就可以了解什么类型你得到的对象。使用 related_name='Category' 有点混乱,因为它不返回 Category 对象。
    • 哦,是的。你说的对。 related_name 应该与 Product 相关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    相关资源
    最近更新 更多