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

307
Views
Autorreferencia del modelo Django: evitar que se haga referencia a sí mismo

Tengo el siguiente modelo:

 class Category(models.Model): name = models.CharField(max_length=255) parent = models.ForeignKey('self', related_name='children')

Mi pregunta es cómo podría evitar que el modelo se haga referencia a sí mismo (mismo objeto). El objeto solo debe poder apuntar a otras categorías pero no a sí mismo (los 'perros' pueden tener 'animales' primarios pero no pueden tener 'perros' primarios)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede anular el método de save para lanzar una excepción:

 def save(self, *args, **kwargs): if self.parent and self.parent.name == self.name: raise ValidationError('You can\'t have yourself as a parent!') return super(Category, self).save(*args, **kwargs)
over 4 years ago · Santiago Trujillo Report

0

Para la interfaz de usuario, se puede restringir el uso limit_choices_to : https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to

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!