是否有可能覆盖Django为UniqueConstraint错误显示的错误消息? (IntegrityError)

问题描述 投票:1回答:1

在多个字段上使用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
django unique-constraint
1个回答
2
投票

您可以捕获异常并使用自定义消息重新提升它:

try:
    # here the statement that rise the exception.
except IntegrityError as err:
    if str(err) == "Skill group with this User and Skill group already exists.":
        raise IntegrityError("Skill Group already in use.")
    # Propagate exception is condition is not matched.
    raise

Update

CreateView继承(以及其他)形式ModelFormMixin,其方法根据docs

form_valid(形式)的

保存表单实例,为视图设置当前对象,并重定向到get_success_url()。

所以你可以:

class YourView(CreateView):

    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.")
            # Propagate exception is candition is not matched.
            raise
         else:
            return result

更新2

也许您可以将代码放在表单的save方法中。

class SkillGroupForm(forms.ModelForm):

    # ...

    def save(**kwargs):
        try:
            result = super().save(**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
© www.soinside.com 2019 - 2024. All rights reserved.