I’m raising ValidationError in my model’s clean() method
from django.core.exceptions import ValidationError
class PosDetail(models.Model, PosDetailChecksMixin):
self._errors = set()
...
def clean(self, *args, **kwargs):
if PosDetail.objects.filter(username=self.username).exists(): # example ValidationError
self._errors.add('check_duplicates')
raise ValidationError("This username is already in use.", code="invalid")
return super().clean(*args, **kwargs)
the ValidationError then is shown in the admin change view form after a POST request.
i want to show a button in the admin change view after form submission
i tried the save_model method but i dose not execute if a ValidationError is raised. i also tried to override the change_view method of the ModelAdmin but it executes before the Model clean method so it misses the validations.
@admin.register(PosDetail)
class BddDetailAdmin(admin.ModelAdmin):
...
def change_view(self, request, object_id, form_url="", extra_context=None):
extra_context = extra_context or {}
pos_detail = self.get_object(request, object_id)
if request.POST:
if pos_detail and 'check_duplicates' in pos_detail._errors:
extra_context['show_save_anyway'] = True
else:
extra_context['show_save_anyway'] = False
return super().change_view(request, object_id, form_url, extra_context)
Is there a ModelAdmin method that runs after Model.clean() ? so i can catch the ValidationError that accur in the Model.clean method.