Использование UniqueConstraint для нескольких полей, и оно работает должным образом, но я хочу переопределить сообщение об ошибке.
Прошло много публикаций, некоторые из них довольно устарели, и ни одна из них не указывает, как это переопределитьконкретный экземпляр.(IntegrityError)
Поля - Пользователь и Группа навыков.Я хотел бы, чтобы в сообщении об ошибке просто говорилось: «Группа навыков уже используется».В настоящее время на нем написано «Группа навыков с этой группой пользователей и группа навыков уже существует».Спасибо.
Обновлено: Думаю, что я должен опубликовать соответствующий код:
models.py
class SkillGroup(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
skill_group = models.CharField(max_length=35)
sequence = models.IntegerField(default=999)
class Meta:
constraints = [
models.UniqueConstraint(fields=['user', 'skill_group'], name='unique_skillgroup'),
]
def __str__(self):
return self.skill_group
def get_absolute_url(self):
return reverse('skillgroup-list')
forms.py
class SkillGroupForm(forms.ModelForm):
class Meta:
model = SkillGroup
fields = ('user', 'skill_group')
widgets = {
'user': forms.HiddenInput,
}
views.py
class SkillGroupCreateView(LoginRequiredMixin, CreateView):
model = SkillGroup
form_class = SkillGroupForm
def get_initial(self):
return {'user': self.request.user}
def form_valid(self, form):
try:
result = super().form_valid(form)
except IntegrityError as err:
if str(err) == "Skill group with this User and Skill group already exists.":
raise IntegrityError("Skill Group already in use.")
raise
else:
return result
IntegrityError at /skillgroup/create/
duplicate key value violates unique constraint "unique_skillgroup"
DETAIL: Key (user_id, skill_group)=(1, Quality Assurance) already exists.
Request Method: POST
Request URL: http://localhost:8000/skillgroup/create/
Django Version: 2.2
Exception Type: IntegrityError
Exception Value:
duplicate key value violates unique constraint "unique_skillgroup"
DETAIL: Key (user_id, skill_group)=(1, Quality Assurance) already exists.
Exception Location: C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\backends\utils.py in _execute, line 84
Python Executable: C:\Users\CHRIST~1\Envs\bcs\Scripts\python.exe
Python Version: 3.7.2
Python Path:
['C:\\Users\\Christopher George\\bcs\\basicrecruiter',
'C:\\Users\\CHRIST~1\\Envs\\bcs\\Scripts\\python37.zip',
'C:\\Users\\CHRIST~1\\Envs\\bcs\\DLLs',
'C:\\Users\\CHRIST~1\\Envs\\bcs\\lib',
'C:\\Users\\CHRIST~1\\Envs\\bcs\\Scripts',
'c:\\users\\christopher '
'george\\appdata\\local\\programs\\python\\python37-32\\Lib',
'c:\\users\\christopher '
'george\\appdata\\local\\programs\\python\\python37-32\\DLLs',
'C:\\Users\\CHRIST~1\\Envs\\bcs',
'C:\\Users\\CHRIST~1\\Envs\\bcs\\lib\\site-packages']
Server time: Thu, 18 Apr 2019 02:53:51 +0000
Traceback:
Environment:
Request Method: POST
Request URL: http://localhost:8000/skillgroup/create/
Django Version: 2.2
Python Version: 3.7.2
Installed Applications:
['recruiter.apps.RecruiterConfig',
'crispy_forms',
'phone_field',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\backends\utils.py" in _execute
84. return self.cursor.execute(sql, params)
The above exception (duplicate key value violates unique constraint "unique_skillgroup"
DETAIL: Key (user_id, skill_group)=(1, Quality Assurance) already exists.
) was the direct cause of the following exception:
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\contrib\auth\mixins.py" in dispatch
52. return super().dispatch(request, *args, **kwargs)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\views\generic\base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\views\generic\edit.py" in post
172. return super().post(request, *args, **kwargs)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\views\generic\edit.py" in post
142. return self.form_valid(form)
File "C:\Users\Christopher George\bcs\basicrecruiter\recruiter\views.py" in form_valid
104. return super().form_valid(form)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\views\generic\edit.py" in form_valid
125. self.object = form.save()
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\forms\models.py" in save
458. self.instance.save()
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\models\base.py" in save
741. force_update=force_update, update_fields=update_fields)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\models\base.py" in save_base
779. force_update, using, update_fields,
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\models\base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\models\base.py" in _do_insert
908. using=using, raw=raw)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\models\query.py" in _insert
1186. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1332. cursor.execute(sql, params)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\backends\utils.py" in execute
99. return super().execute(sql, params)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\backends\utils.py" in execute
67. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
76. return executor(sql, params, many, context)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\backends\utils.py" in _execute
84. return self.cursor.execute(sql, params)
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\CHRIST~1\Envs\bcs\lib\site-packages\django\db\backends\utils.py" in _execute
84. return self.cursor.execute(sql, params)
Exception Type: IntegrityError at /skillgroup/create/
Exception Value: duplicate key value violates unique constraint "unique_skillgroup"
DETAIL: Key (user_id, skill_group)=(1, Quality Assurance) already exists.
Обновление 2: На основе некоторыхчитая документацию по Django, я попытался изменить его на безуспешно:
def save(self, *args, **kwargs):
try:
result = super().save(*args, **kwargs)
except IntegrityError as err:
if str(err) == "Skill group with this User and Skill group already exists.":
raise IntegrityError("Skill Group already in use.")
raise
else:
return result