I’m defining my Django models right now and I realized that there wasn’t a OneToManyField in the model field types. I’m sure there’s a way to do this, so I’m not sure what I’m missing. I essentially have something like this:
class Dude(models.Model):
# 1 dude can have 0+ phone numbers
numbers = models.OneToManyField('PhoneNumber')
class PhoneNumber(models.Model):
number = models.CharField()
In this case, each Dude can have multiple PhoneNumbers, but the relationship should be unidirectional, in that I don’t need to know from the PhoneNumber which Dude owns it, per se, as I might have many different objects that own PhoneNumber instances, such as a Business for example:
class Business(models.Model):
numbers = models.OneToManyField('PhoneNumber')
What would I replace OneToManyField (which doesn’t exist) with in the model to represent this kind of relationship? I’m coming from Hibernate/JPA where declaring a one-to-many relationship was as easy as:
@OneToMany
private List<PhoneNumber> phoneNumbers;
How can I express this in Django?
To handle One-To-Many relationships in Django you need to use
ForeignKey.The documentation on
ForeignKeyis very comprehensive and should answer all the questions you have:https://docs.djangoproject.com/en/3.2/ref/models/fields/#foreignkey
The current structure in your example allows each
Dudeto have one number, and each number to belong to multipleDudes (same withBusiness).If you want the reverse relationship, you would need to add two
ForeignKeyfields to yourPhoneNumbermodel, one toDudeand one toBusiness. This would allow each number to belong to either oneDudeor oneBusiness, and haveDudes andBusinesses able to own multiplePhoneNumbers. I think this might be what you’re after: