[Answer]-Django – Is ManyToManyField required to use over ForeignKey?

1👍

ForeignKey and ManyToMany are two different things. You probably mean ForeignKey v/s OneToOne

An example:

Lets take some arbitrary Developer model

class Developer(models.Model):
    user = models.OneToOneField(User) #ensure there is a one-to-one relationship between User and Developer model - One user object in django.contrib.auth can be associated with only one Developer and vice versa 
    category = models.ManyToMany(Category) #Developer can be part of many categories, and also one category can be associated with many developers
    birth_address = models.ForeignKey(Address) #He can have only birth place. The address can be associated with many, so it is a `OneToMany` relationship

So, ManyToMany is many-to-many relationship and ForeignKey is a restricted ManyToMany (a many-to-one relationship).

So, you can use either one, but might be a good idea to restrict to ForeignKey if you wish to restrict the number of objects that can be associated with the Model in question

Leave a comment