I want to know if it is possible (and if so, what is the most pythonic way) to have a product that belongs to multiple product categories. e.g. an avocado belonging to [Food->Fresh Food->Fruit & Vegetables->Salad & Herbs] as well [Food->Pantry->Global Cuisine->Mexican]?
I am currently using Django-MPTT to create the category tree for single-category products. How do I set up the Product and Category models, what would the data import look like, and what would an efficient query look like? I am open to any suggestion, even if it requires me to rewrite my project. I would appreciate ANY advice on anything to do with this problem.
I am currently using Django-MPTT to create the category tree for single-category products. It makes sense conceptually, but I'm struggling with the multiple-category tree.
I've found some examples of TreeManyToManyField
class Category(MPTTModel, CreationModificationDateMixin):
parent = TreeForeignKey('self',
on_delete=models.CASCADE,
blank=True,
null=True)
title = models.CharField(max_length=200)
def __str__(self):
return self.title
class Meta:
ordering = ['tree_id', 'lft']
verbose_name_plural = 'categories'
class Product(CreationModificationDateMixin):
title = models.CharField(max_length=255)
categories = TreeManyToManyField(Category)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = 'movies'
but there are no explanations or examples of import/creating data for such a model. I have also seen the following example of how to set up the Category model, but they use a BaseProduct which threw me off:
class CategoryProductBase(BaseProduct):
main_category = TreeForeignKey(get_category_model_string('Category'))
additional_categories = TreeManyToManyField(
get_category_model_string('Category'),
related_name='extra_product_categories')
Aucun commentaire:
Enregistrer un commentaire