[Django]-Django pre_save signal and ModelAdmin custom error message

0👍

Not sure if this is still relevant.
But the way I solved this is by creating a ModelForm:

class AuthorAdminForm(forms.ModelForm):

    def clean(self):
        # or some api calls here
        if self.instance.id > 4:
            self.instance.name = "4+"
        else:
            ValidationError("Id is bigger that 4")
        return super().clean()

And then by adding the form to the admin model:

class AuthorAdmin(admin.ModelAdmin):
    form = AuthorAdminForm

This clean() method will ensure that you could add/modify fields before you hit the model save() method and still throw a ValidationError if something goes south like a 404 error when hitting a url

Leave a comment