models.py
class Product(models.Model):
.
.
pass
class Color(models.Model):
color_name = models.CharField(blank=True, max_length=50)
color = ColorField(default='#FF0000')
class ProductColor(models.Model):
product = models.ForeignKey('Product', on_delete=models.CASCADE)
color = models.ForeignKey('Color', on_delete=models.CASCADE)
filters.py
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = ['color',]
i want to filter product by color using django_filters how should i do??
我想用django_filters按颜色过滤产品我该怎么办?
2 个解决方案
#1
0
You can add a new filter to your filters.py
file, but instead refer to the ProductColor
model.
您可以向filters.py文件添加新过滤器,但请参阅ProductColor模型。
filters.py
class ProductFilter(django_filters.FilterSet):
productcolor__color__color = django_filters.CharFilter(lookup_expr='iexact')
productcolor__color__color_name = django_filters.CharFilter(lookup_expr='iexact')
class Meta:
model = Product
Note that I'm using productcolor__color__color
to refer to the color
property in the Color
model. The double underscore access a ForeignKey property.
请注意,我使用productcolor__color__color来引用Color模型中的color属性。双下划线访问ForeignKey属性。
The lookup_expr='iexact'
will match both lowercase and uppercase color code (#fff* or #FFF).
lookup_expr ='iexact'将匹配小写和大写颜色代码(#ffff *或#FFF)。
Then a simple HTML to filter your queryset could be:
然后,用于过滤查询集的简单HTML可以是:
<form method="get">
{{ filter.form.as_p }}
<button type="submit">Search</button>
</form>
<ul>
{% for product in filter.qs %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
The {{ product.name }}
is just an example, I don't know if your Product
model have a name
field. But you can access your product following the {{ product.<field_name> }}
.
{{product.name}}只是一个示例,我不知道您的产品型号是否有名称字段。但您可以按照{{product。
You can read more about django_filters here: https://simpleisbetterthancomplex.com/tutorial/2016/11/28/how-to-filter-querysets-dynamically.html
你可以在这里阅读更多关于django_filters的信息:https://simpleisbetterthancomplex.com/tutorial/2016/11/28/how-to-filter-querysets-dynamically.html
#2
0
Inside your view, you can make a query like this
在您的视图中,您可以进行这样的查询
c = Color('my desired color', '#ffffff')
result = ProductColor.objects.filter(color=c.color)
for r in result:
# do something ...
#1
0
You can add a new filter to your filters.py
file, but instead refer to the ProductColor
model.
您可以向filters.py文件添加新过滤器,但请参阅ProductColor模型。
filters.py
class ProductFilter(django_filters.FilterSet):
productcolor__color__color = django_filters.CharFilter(lookup_expr='iexact')
productcolor__color__color_name = django_filters.CharFilter(lookup_expr='iexact')
class Meta:
model = Product
Note that I'm using productcolor__color__color
to refer to the color
property in the Color
model. The double underscore access a ForeignKey property.
请注意,我使用productcolor__color__color来引用Color模型中的color属性。双下划线访问ForeignKey属性。
The lookup_expr='iexact'
will match both lowercase and uppercase color code (#fff* or #FFF).
lookup_expr ='iexact'将匹配小写和大写颜色代码(#ffff *或#FFF)。
Then a simple HTML to filter your queryset could be:
然后,用于过滤查询集的简单HTML可以是:
<form method="get">
{{ filter.form.as_p }}
<button type="submit">Search</button>
</form>
<ul>
{% for product in filter.qs %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
The {{ product.name }}
is just an example, I don't know if your Product
model have a name
field. But you can access your product following the {{ product.<field_name> }}
.
{{product.name}}只是一个示例,我不知道您的产品型号是否有名称字段。但您可以按照{{product。
You can read more about django_filters here: https://simpleisbetterthancomplex.com/tutorial/2016/11/28/how-to-filter-querysets-dynamically.html
你可以在这里阅读更多关于django_filters的信息:https://simpleisbetterthancomplex.com/tutorial/2016/11/28/how-to-filter-querysets-dynamically.html
#2
0
Inside your view, you can make a query like this
在您的视图中,您可以进行这样的查询
c = Color('my desired color', '#ffffff')
result = ProductColor.objects.filter(color=c.color)
for r in result:
# do something ...