nucklehedd
nucklehedd

Reputation: 301

In Django, I want to implement a One-To-Many Relationship

I've read of folks talking about using ForeignKey for a One-To-Many relationship in Django, but that's not what I'm looking for. Suppose I have a model called Club defined simply as:

class Club(models.Model):
  name = models.CharField(max_length = 64)

What I want is to add a field called owners that is a one-to-many relationship to users (the User model). I don't want to add a ForeignKey to User that references Club because that will clutter User. I really want a field in the Club model that maintains the relationship to 0, 1, or more Users that are considered owners of the Club. Anyone have any ideas?

Upvotes: 0

Views: 324

Answers (1)

miku
miku

Reputation: 188194

What you are looking for is a Many-To-Many relationship.

In a many-to-many relationship, each row in table A can have many (zero or more) matching rows in table B, and each row in table B can have many matching rows in table A. Each club can have many owners, and each owner can have many clubs, for example.

Adapted from: http://my.safaribooksonline.com/book/databases/sql/9780321584069

Upvotes: 3

Related Questions