0

I have a function that changes the details of my models when triggered. I cannot get it to do the work I intend to have it do. I can't tell why this is so. Any help will greately be appreciated.

The error code:

 @login_required(login_url='/login/') def move_students(request): try: Klass.objects.filter(school=request.user.school,name=1).update(name=2) except Exception as e: messages.info(request,f'erroerd at {e}') return redirect('/') 

Model:

 class Klass(models.Model): name = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(5),], help_text='E.g 1,2,3, 4') school = models.ForeignKey(School,on_delete=models.CASCADE) def __str__(self): return str(self.name) class Meta: verbose_name_plural = 'Classes' unique_together = ("school", "name") 

Stacktrace:

 Traceback (most recent call last): File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view return view_func(request, *args, **kwargs) File "D:\Python\Django\Completed Projects\lib_system\Library-System\libman\views.py", line 29, in move_students Klass.objects.filter(school=request.user.school,name=1).update(name=2) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 784, in update rows = query.get_compiler(self.db).execute_sql(CURSOR) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\compiler.py", line 1522, in execute_sql cursor = super().execute_sql(result_type) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\compiler.py", line 1156, in execute_sql cursor.execute(sql, params) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 98, in execute return super().execute(sql, params) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "C:\Users\FR GULIK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: UNIQUE constraint failed: libman_klass.school_id, libman_klass.name [09/Jun/2021 11:14:57] "GET /library/move/students HTTP/1.1" 500 125470 

1 Answer 1

2

The error is telling you: django.db.utils.IntegrityError: UNIQUE constraint failed: libman_klass.school_id, libman_klass.name. This means that the unique_together on your models is throwing an error as you are trying to save a school and name which already exists in the database.

For example the code below, a Klass with the school=request.user.school and name=2 already exists but you have a unique together constraint to prevent this.

Klass.objects.filter(school=request.user.school,name=1).update(name=2) 

If you don't want this, remove the unique_together constraint and run migrations again (./manage.py makemigrations ./manage.py migrate) or except an IntegrityError:

try: Klass.objects.filter( school=request.user.school, name=1 ).update(name=2) except IntegrityError as e: # do something messages.info(request,f'erroerd at {e}') return redirect('/') 
Sign up to request clarification or add additional context in comments.

2 Comments

You need to remove the unique_together inside your model and run migrations again. Or you can except an IntegrityError and handle it.
I've updated the answer with the IntegrityError handling.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.