我需要每张Food
的几张照片。设计模型有多好?
这是我的尝试,但这不是一件好事:
class Photo(models.Model):
img = models.ImageField(upload_to="media/images")
class Food(models.Model):
name = models.CharField(max_length=100)
desc = models.TextField()
photo = models.ManyToManyField(Photo)
答案 0 :(得分:3)
在我看来,外键会更好:
class Photo(models.Model):
img = models.ImageField(upload_to="media/images")
food = models.ForeignKey(Food)
class Food(models.Model):
name = models.CharField(max_length=100)
desc = models.TextField()
因为一张照片总是用于特定食物