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

352
Views
¿Cuál es el motivo de IntegrityError: la restricción NOT NULL falló?

Estoy haciendo las aplicaciones de encuestas en Django Documentation: Writing your first Django app, part 2 . Mientras trabajaba con la API de la base de datos, encontré un IntegrityError.

encuestas/modelos.py

 class Question(models.Model): question_text=models.CharField(max_length=200) pub_date=models.DateTimeField('date published')

Intenté crear un objeto del modelo Pregunta.

 Question.objects.create(question_text="What's up?")

Esto da un error

IntegrityError: restricción NOT NULL falló: polls_question.pub_date

Pero cuando intento esto

 Question.objects.create(pub_date=timezone.now())

Crea un objeto con éxito.

¿Cuál es el motivo de IntegrityError en el primer caso y por qué no genera ningún error en el segundo caso?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

De django doc

Evite usar null en campos basados en cadenas como CharField y TextField. Si un campo basado en cadenas tiene null=True, eso significa que tiene dos valores posibles para "sin datos": NULL y la cadena vacía. En la mayoría de los casos, es redundante tener dos valores posibles para "sin datos"; la convención de Django es usar la cadena vacía, no NULL.

  • Entonces, si null=False (predeterminado), Django usará una cadena vacía (significa que no es NULL)
  • Si null=True , Field puede escribir NULL (significa que None llega a python)

En resumen : Django no generará la IntegrityError: NOT NULL constraint para CharField y TextField .

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!