Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
How to create model with relationship one-to-many in Django?

I need create relationship one-to-many. One Group can have many members.

Did I do it right?

Model:

class Group(models.Model):
    id = models.BigAutoField(primary_key=True)
    groupName: models.CharField(max_length=100)
    description: models.CharField(max_length=255)
    createdAt = models.DateTimeField(auto_now_add=True)
    updatedAt = models.DateTimeField(auto_now=True)
    members: models.ForeignKey(User, n_delete=models.CASCADE)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

ForeignKey represents ManyToOne relationship so here according to your model One member will have many groups.

If you want one group will have many members then the models should be like this.


class Group(models.Model):
    id = models.BigAutoField(primary_key=True)
    groupName: models.CharField(max_length=100)
    description: models.CharField(max_length=255)
    createdAt = models.DateTimeField(auto_now_add=True)
    updatedAt = models.DateTimeField(auto_now=True)

class Member(models.Model):
   group = models.ForeignKey(Group)
   ......
    
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!