Nullpoet
Nullpoet

Reputation: 11259

django models : How to have variable number of foreign keys in a model?

For instance, Model Resume contains variable number of Model Project 's,
What should be my models and relationships between them to achieve this ?
Thanks in advance.

Upvotes: 3

Views: 1246

Answers (2)

satoru
satoru

Reputation: 33225

Seems to me that what you need is a many-to-many relationship between Resume and Project, so I would suggest doing something like this:

class Project(models.Model):
    # Project fields

class Resume(models.Model):
    # Resume fields
    projects = models.ManyToManyFields(Project, related_name='resumes')

Note that a default association table will be defined by Django under the hood this way.

And now you have a model in which a resume can be related with multiple projects and vice versa.

Upvotes: 3

Olivier Verdier
Olivier Verdier

Reputation: 49146

You just need either a many to many field, or a foreign key from Model Project to Model Resume.

Upvotes: 2

Related Questions