实现django-filter MultipleModelChoiceField以进行反向关系查找?

时间:2018-03-15 11:10:23

标签: python django django-filter

我一直在为我的一个项目使用django-filters。根据文档,我为我的产品模型实现了产品过滤器(用于颜色和材料)。但经过多次尝试后,我仍无法对另一个表ProductSize中的产品大小进行反向查找。

models.py

class Product(models.Model):
    product_name = models.CharField(max_length=500)
    product_color = models.ForeignKey(Colour,related_name='productcolor')
    product_material=models.ForeignKey(Material)

    def __str__(self):
        return self.product_name

class ProductSize(models.Model):
    product=models.ForeignKey('Product',related_name='details')
    value = models.CharField(max_length=50)
    stock= models.IntegerField(default=1)
    items_sold = models.IntegerField(default=0)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return self.value 

class Colour(models.Model):
    colour=models.CharField(max_length=100)
    colour_code=models.CharField(max_length=100,null=True,blank=True)

    def __unicode__(self):
        return u'%s' %self.colour

class Material(models.Model):
    material=models.CharField(max_length=100)

    def __unicode__(self):
        return u'%s' %self.material

filters.py

class ProductFilter(django_filters.FilterSet):
    product_material=django_filters.ModelMultipleChoiceFilter(
        queryset=Material.objects.all(),
        widget=forms.CheckboxSelectMultiple
    )
    product_color=django_filters.ModelMultipleChoiceFilter(
        queryset=Colour.objects.all(),
        widget=forms.CheckboxSelectMultiple
    )

    class Meta:
        model = Product
        fields=['product_color','product_material']

views.py

def product_view(request):
    product=Product.objest.all()
    f = ProductMaterialFilter(request.GET, queryset=product)
    context={'filter':f,}
    return render(request,'product.html',context)

Django版本:

django versions==1.10.8
django-filter==1.1.0

我需要通过' value'来了解尺寸过滤的步骤。 ProductSize的{​​{1}}个模型。

See here

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

models.py

from django.db import models


class Colour(models.Model):
    colour = models.CharField(max_length=100)
    colour_code = models.CharField(max_length=100,null=True,blank=True)

    def __unicode__(self):
        return u'%s' %self.colour


class Material(models.Model):
    material = models.CharField(max_length=100)

    def __unicode__(self):
        return u'%s' %self.material


class Size(models.Model):
    value = models.CharField(unique=True, max_length=4)

    def __unicode__(self):
        return self.value


class Product(models.Model):
    product_name = models.CharField(max_length=500)
    product_color = models.ForeignKey(Colour,related_name='productcolor')
    product_material = models.ForeignKey(Material)

    def __str__(self):
        return "{} ({}, {})".format(self.product_name, self.product_color, self.product_material)


class Stock(models.Model):
    product = models.ForeignKey('Product', related_name='details')
    size = models.ForeignKey('Size')
    stock = models.IntegerField(default=1)
    items_sold = models.IntegerField(default=0)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return "{product_name}({material}, {color}) @size: {size}, available {available} out of: {stock}".format(
            product_name=self.size.value,
            material=self.product.product_material.material,
            color=self.product.product_color.colour,
            size=self.size.value,
            available=self.stock-self.items_sold,
            stock=self.stock
        )

filters.py

import django_filters
from django import forms

from products.models import Material, Colour, Stock, Size


class ProductFilter(django_filters.FilterSet):
    product__product_material = django_filters.ModelMultipleChoiceFilter(
        queryset=Material.objects.all(),
        widget=forms.CheckboxSelectMultiple
    )
    product__product_color = django_filters.ModelMultipleChoiceFilter(
        queryset=Colour.objects.all(),
        widget=forms.CheckboxSelectMultiple
    )

    size = django_filters.ModelMultipleChoiceFilter(
        queryset=Size.objects.all(),
        widget=forms.CheckboxSelectMultiple
    )

    class Meta:
        model = Stock
        fields = ['product__product_color', 'product__product_material', 'size']

演示:

https://github.com/andilabs/filters-demo