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

147
Views
Django user ForeignKey and unique object name
class User:
    username = (unique=True)

class Object:
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    name = ()

How can i have multiple Objects with the same name, but each user can only have one unique Object name.

for example:

user1 can only have one unique Object name of "dog", user2 can also only have one unique Object name of "dog", therefore there can be multiple Objects with the same name of "dog", but each user can only have one Object named "dog" through the ForeignKey. if user1 try's to create another object named "dog" then raise something like a forms validation error.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use the unique_together option to add the database constraint.

class MyModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    name = models.CharField(max_length=30)

    class Meta:
        unique_together = ['user', 'name']
about 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!