From 42deebb7582efe1c46ac81c01642abf510dd96d2 Mon Sep 17 00:00:00 2001 From: jhyns Date: Wed, 3 May 2023 14:09:38 +0900 Subject: [PATCH] Add related name on models in market app --- market/models.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/market/models.py b/market/models.py index 61465ab..c2dcacd 100644 --- a/market/models.py +++ b/market/models.py @@ -27,9 +27,15 @@ class ProductStorage(models.Model): class Product(models.Model): name = models.CharField(max_length=50) - brand = models.ForeignKey(Brand, on_delete=models.CASCADE) - color = models.ManyToManyField("ProductColor", blank=True) - storage = models.ManyToManyField("ProductStorage", blank=True) + brand = models.ForeignKey( + Brand, on_delete=models.CASCADE, related_name="products" + ) + color = models.ManyToManyField( + "ProductColor", blank=True, related_name="products" + ) + storage = models.ManyToManyField( + "ProductStorage", blank=True, related_name="products" + ) release_date = models.DateField(blank=True, null=True) def __str__(self): @@ -42,7 +48,9 @@ class Post(models.Model): ("r", "reserved"), ("d", "done"), ) - product = models.ForeignKey(Product, on_delete=models.CASCADE) + product = models.ForeignKey( + Product, on_delete=models.CASCADE, related_name="posts" + ) color = models.ForeignKey(ProductColor, on_delete=models.CASCADE) storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE) price = models.IntegerField() @@ -58,7 +66,9 @@ class Post(models.Model): class Image(models.Model): - post = models.ForeignKey(Post, on_delete=models.CASCADE) + post = models.ForeignKey( + Post, on_delete=models.CASCADE, related_name="images" + ) image = models.ImageField(upload_to="images", blank=True, null=True) def __str__(self):