Home > Net >  How to solve a problem of IntegrityError?
How to solve a problem of IntegrityError?

Time:12-26

I need your help to solve this problem. I am creating a django application that manages absences and notes for an establishment. I wanted to create a list form with which the teacher can record absences. But I encountered an error in saving the view form. Here is my model and the view.

----Table:

class Absence(models.Model):
    matricule = models.ForeignKey("Etudiant", on_delete=models.CASCADE)
    date = models.ForeignKey("Cours", on_delete=models.CASCADE)
    nombre_heure= models.IntegerField(null=True, default="0")

----Views:

def listeappel(request):
    matricule=request.POST.get('matricule')
    date=request.POST.get('date')
    nombre_heure=request.POST.get('nombre_heure')
    newAbsence=Absence.objects.create(matricule=matricule, date=date, nombre_heure=nombre_heure)
    newAbsence.save() 
    return render(request, 'mesabsences/listeappel.html')

---error :

File "C:\Users\ISSA\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers  
    return executor(sql, params, many, context)
  File "C:\Users\ISSA\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\ISSA\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\ISSA\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\ISSA\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: note_absence.date_id
[25/Dec/2022 09:24:41] "GET /note/listeappel HTTP/1.1" 500 148241

I tried to delete the migration file and the database and then redo the migration, but it didn't work.

CodePudding user response:

The error message says: "IntegrityError: NOT NULL constraint failed: note_absence.date_id". it looks like there is no date value in the post data. check the request.POST.

also. You are trying to create a Absence object twice. remove newAbsence.save() line in the view. like this:

def listeappel(request):
    matricule=request.POST.get('matricule')
    date=request.POST.get('date')
    nombre_heure=request.POST.get('nombre_heure')
    newAbsence=Absence.objects.create(matricule=matricule, date=date, nombre_heure=nombre_heure)
    return render(request, 'mesabsences/listeappel.html')

when you create an object with create method, it saves the object in the database automatically so no need to call the save() method again.

CodePudding user response:

In fact, I wanted to make the form like a call list of absences where in front of the name of each student, there is an "absent" button. I tried to add a value to the post data, but it doesn't work. I also deleted newAbsence.save().

  • Related