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

248
Views
Django - One to Many relationship

I have user and user role models. There are 3 user roles as teacher, student, admin. One user should have only one role but if I make one-to-one relationship only one user can be attached to one role. I want there can be many students, many teacher, many admins but one user must only have one role. I couldn't figure out how to design this relationship.

I have 2 models as User and User_Role.

class role(models.Model):
   role_name = models.CharField(max_length=64)

class user(models.Model):
   name = models.CharField(max_length=64)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You should define a ForeignKey from the User model to the Role, so:

class Role(models.Model):
    role_name = models.CharField(max_length=64)

class User(models.Model):
    name = models.CharField(max_length=64)
    role = models.ForeignKey(Role, on_delete=models.PROTECT)

Note: Models in Django are written in PascalCase, not snake_case, so you might want to rename the model from user to User.

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!