django 按类别在颜色过滤器中获取产品颜色

oo7oh9g9  于 2022-12-05  发布在  Go
关注(0)|答案(2)|浏览(127)

我试图得到一个特定类别的产品类别鼻涕虫。我有颜色模型,产品模型和产品变化模型在商店应用程序。
第一个
在我的网站上views.py,

def shop(request,category_slug=None):

    categories = None
    products = None
    if category_slug != None:
        categories = get_object_or_404(Category,slug = category_slug)
        products = Product.objects.filter(category=categories,is_available=True).order_by('id')
        variation = ProductVaraiant.objects.filter(product__category = categories)
        print(variation)
        # color = color.objects.all()
        products_count = products.count()
    else:
        products = Product.objects.all().filter(is_available=True).order_by('id')
        products_count = products.count()
        variation = ProductVaraiant.objects.all()
        print(variation)

    context = {
        'products' : products,
        'products_count' : products_count,
        'variation' : variation
    }
    return render(request,'shop/shop.html',context)

我分类模型,

class Category(MPTTModel):
    parent = TreeForeignKey('self',blank=True,null=True,related_name='children',on_delete=models.CASCADE)
    category_name = models.CharField(max_length=200,unique=True)
    category_img = models.ImageField(upload_to='photos/categories',blank=True)
    slug = models.SlugField(max_length=100,unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def img_preview(self): 
        return mark_safe('<img src = "{url}" width = "50" height = "50"/>'.format(
             url = self.category_img.url
         ))

    def __str__(self):
        return self.category_name

    class Meta:
        verbose_name_plural = 'categories'

    class MPTTMeta:
        order_insertion_by = ['category_name']

我想得到的是,就像我有3个子类别。每个类别将有任何color.So产品,如果我按类别过滤产品,颜色将显示在该类别产品的侧栏中,而不会避免重复,因为许多产品可能有相同的color.So,我得到相同的颜色多次。如果我需要使用不同的(),如何在查询中使用它,根据产品类别删除重复的颜色。我在模板中尝试了这个

<form>
                    <div class="custom-control custom-checkbox d-flex align-items-center justify-content-between mb-3">
                        <input type="checkbox" class="custom-control-input" checked id="color-all">
                        <label class="custom-control-label" for="price-all">All Color</label>
                        <span class="badge border font-weight-normal">1000</span>
                    </div>
                    {% for i in variation %}
                    {% ifchanged i.color %}
                    <div class="custom-control custom-checkbox d-flex align-items-center justify-content-between mb-3">
                        <input type="checkbox" class="custom-control-input filter-checkbox" id="{{i.color.id}}" data-filter="color">
                        <label class="custom-control-label" for="{{i.color.id}}">{{i.color}}</label>
                        <span class="badge border font-weight-normal">150</span>
                    </div>
                    {% endifchanged %}
                    {% endfor %}
                </form>

但是,它只是删除重复的最后一次迭代。如何避免得到重复的所有颜色?

rekjcdws

rekjcdws1#

就在你有一行# color = color.objects.all()的地方,把它改为color = variation.color.all().distinct('id'),然后把它传递给你的模板。

tjrkku2a

tjrkku2a2#

回答我的问题,我做了什么:

variation = ProductVaraiant.objects.filter(product__category = categories)

我改变了什么:

variation = ProductVaraiant.objects.filter(product__category=categories).values('color__title').distinct()

如果使用postgre数据库,则不需要使用值,只需使用distinct('color '),您将需要使用默认数据库的值以避免以下错误,

DISTINCT ON fields is not supported by this database backend

相关问题