Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

355
Vistas
What's the reason for IntegrityError: NOT NULL constraint failed?

I'm doing the polls apps in Django Documentation: Writing your first Django app, part 2. While working with Database API, I've found an IntegrityError.

polls/models.py

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

I've tried to create an object of Question model.

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

This gives an error

IntegrityError: NOT NULL constraint failed: polls_question.pub_date

But when i try this

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

It creates an object successfully.

What's the reason for IntegrityError in the first case and why it doesn't generate any error in the second case?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

From django doc

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

  • So if null=False(default), django will use empty string (means not NULL)
  • If null=True, Field can write NULL (means None comes to python)

So in brief: Django will not raise IntegrityError: NOT NULL constraint for CharField and TextField.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda