[Python] Django Error Message From django.db Models To Admin

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using DRF to create an API backend. Now, I want to enforce certain validation rules while saving the models from the admin. I'm using viewset and can override when called from the rest framework. But when I validate model by overriding the save() method, it can't send any error message to the admin as it doesn't have any request parameter. The scenario is like this:

    from django.db import models

    class PatientCaregiver(models.Model):
    name = models.CharField(max_length=200, null=True, blank=True)
    email = models.CharField(max_length=200, null=True, blank=True)

    def save(self, *args, **kwargs):
    # do some processing
    # return some error or success msg(how?)


    I know that I can send the error message with the following code:

    from django.contrib import messages
    messages.error(request,'Error message')


    But as you can see, in my save() model, I don't have the request parameter. So, how to send any error message from the save() model to the admin?

    Continue reading...

Compartilhe esta Página