Let’s say we have a model called Promotions, and we want our Promotions to have many products and product to have many Promotions, it is a many to many relationship. In this case, we create the relationship in the child class or Product class since we want our users to view products alongside promotions so it makes the most sense. Django will create the inverse relationship in the Promotion class anyway.
class Promotion(models.Model): description = models.Charfield(max_length=255) discount = models.FloatField()
class Product(models.Model): title = models.CharField(max_length=255) description = models.TextField() price = models.DecimalField(max_digits=6, decimal_places=2) inventory = models.IntegerField() last_update = models.DateTimeField(auto_now=True)
# 1toMany rlnshp with Collection collection = models.ForeignKey(Collection, on_delete=models.PROTECT)
#manytomany relationship promotions = models.ManyToManyField(Promotion)