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

130
Views
¿Se supone que "max_length" y "choices" en la definición de clase de Model evitan la inserción de valores no válidos en la base de datos?

Tengo una situación en la que tengo un modelo como:

 class Box(models.Model): BOX_CHOICES = [('L','Large'),('M','Medium'),('S','Small')] size= models.CharField(max_length=1,choices=BOX_CHOICES, blank=True, null=True)

Pensé que esto aseguraría que no pudiera agregar una cadena como "Descomunal" en el campo de tamaño. Y, sin embargo, pude lograr exactamente eso usando la función get_or_create para insertar.
¿Podría explicar por qué max_length y las opciones no restringieron la inserción?
¡Gracias!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

get_or_create() (como create() ) no llama full_clean() , la función de validación que verifica cosas como opciones, max_length, etc. Por lo tanto, deberá ejecutarlo usted mismo:

 try: box = Box.objects.get(**your_get_values) except Box.DoesNotExist: box = Box(**your_get_values, **any_other_values) box.full_clean() box.save()
over 4 years ago · Santiago Trujillo Report

0

Los validadores max_length y choice son llamados por full_clean, como se explica en la documentación. Puede llamar a este método manualmente:

 from django.core.exceptions import ValidationError try: box = Box.objects.create(size="L") box.full_clean() box.save() except ValidationError: pass
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!