Products catalogue: filter by parameters
I need to make the filter products by features.
So, products model:
class Product(models.Model): name = models.CharField(max_length=255, unique=True')
And feature models:
class Value(models.Model): value = models.CharField(max_length=50) class FeatureName(models.Model): name = models.CharField(max_length=50) class Feature(models.Model): name = models.ForeignKey(FeatureName) value = models.ForeignKey(Value) item = models.ForeignKey(Product)
To mark up a template form of filtering I need to get all the possible names of the characteristics and values of this characteristic.
Like this:
Color: Red, White, Blue Size: 1, 2, 3
I hope somebody understood me, tell me how to do clever to realize a functional. Thanks:)
Start with listing all Features for given product:
product = Product.objects.get(pk=given_pk)
features = product.feature_set.all().select_related()
Now group your features directly in Python.
features_dict = {}
for feature in features:
values = features_dict.get(feature.name.name, [])
features_dict[feature.name.name] = values + [feature.value.value]
That will give you dict linking all name to it's existing values.
链接地址: http://www.djcxy.com/p/38680.html上一篇: Django ImageField在空时覆盖现有的路径
下一篇: 产品目录:按参数过滤