I have two models in my models.py file:
Flavour model:
class Flavour(models.Model):
flavour_choice = models.CharField(null=True, blank=True, max_length=254)
def __str__(self):
return self.flavour_choice
and Product model:
class Product(models.Model):
category = models.ForeignKey(
'Category', null=True, blank=True, on_delete=models.SET_NULL
)
slug = models.SlugField()
sku = models.CharField(max_length=254, null=True, blank=True)
name = models.CharField(max_length=254)
brand = models.TextField()
has_flavours = models.BooleanField(default=False, null=True, blank=True)
flavours = models.ForeignKey(
'Flavour', null=True, blank=True, on_delete=models.SET_NULL
)
has_strength = models.BooleanField(default=False, null=True, blank=True)
strength = models.ForeignKey(
'Strength', null=True, blank=True, on_delete=models.SET_NULL
)
description = models.TextField()
price = models.DecimalField(max_digits=6, decimal_places=2)
rating = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
image_url = models.URLField(max_length=1024, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
display_home = models.BooleanField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-created_at',)
def __str__(self):
return self.name
I want to able to add flavours to the flavours table and then choose if they appear for particular products. How would I go about this? I know I could just add the flavours to the product but so many of the products I want to have, have the same flavours.
I need to be able to do this database side and not just programmatically so admin users can add flavours and products via a front-end product management page.
3
Answers
I went a different route in the end as I was overcomplicating the issue.
I added a Variation model:
And now I can just use an if statement to call the flavours into the template in a for loop that iterates over all the flavours associated with the product in the product variation model.
You can use a ManyToMany relationship, where many flavours can be used for many different products.
More explanation here : https://docs.djangoproject.com/en/4.1/topics/db/examples/many_to_many/
First about your models and fields. You can use model choices at
Flavour
model, but it is not necessarily required. You can get rid ofhas_flavours
andhas_strength
fields, use a model property andflavours
andstrengths
relations to obtain the desired output.Also, as mentioned on the other answer, substitute the relation between
Flavour
andProduct
with a many-to-many relation in order to avoid duplicates in your database. Lastly,image_url
is also not necessary, since you are usingmodels.ImageField
it is possible to access the image url via attribute e.g.instance.image.url
.models.py
A simple example, on how you would associate a flavour with a specific product:
tests.py
I would also consider the possibility of creating a model for
brand = models.TextField()
with aone-to-many
relation.