Julie
Julie

Reputation: 125

Few photos of each Food. How to design model?

I need a few photos of each Food. How well design model?

This is my try, but it is not a good:

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)

Upvotes: 1

Views: 87

Answers (1)

Lukasz Koziara
Lukasz Koziara

Reputation: 4320

In my opinion ForeignKey will be better:

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()

bacause one photo is always for specific food

Upvotes: 3

Related Questions