我有一个产品:
class Product(models.Model):
...
此产品的功能,fk为动态属性:
class ProductFeatures(models.Model):
product = models.ForeignKey('Product')
features_category = models.ForeignKey('FeaturesCategory')
value = models.ForeignKey('Features')
动态属性名称模型:
class FeaturesCategory(models.Model):
title = models.CharField(max_length=255, verbose_name=_(u'Название'))
每个属性的值:
class Features(models.Model):
category = models.ForeignKey('FeaturesCategory')
value = models.CharField(max_length=255, verbose_name=_(u'Значение'))
完成所有这些工作的例子:
我们创建了FeaturesCategory对象 - " Color",为其添加了一些值:" Red"," Green"," Blue"。并添加了另一个FeaturesCategory对象 - " Count"值:" 1"," 2"," 3"
我们将所有这些添加到产品中,例如:
颜色 - 红色 数 - 3
麻烦,是如何过滤这个产品的?
for param in self.request.GET:
if not param in ['pangination', 'view', 'order', 'page']:
name, value = param.split('_')
filters.append(
Q(productfeatures__features_category__title=name,
productfeatures__features_category__features__value=value)
)
如果我们有两个" params"过滤它不起作用
答案 0 :(得分:0)
你应该动态建立类似的东西:
qlist = []
for fi in filters:
if len(fi) == 2:
fi = fi.split('_')
qlist.append(Q((fi[0],fi[1])))
if qlist:
YourModel.objects.filter(reduce(operator.and_, qlist))