A
A
Animkim2016-05-13 06:57:29
Django
Animkim, 2016-05-13 06:57:29

How to send a message to django admin?

Django admin shows messages like "category "Photo" has been successfully added."
How can I send my messages directly from the model, that is, the user saves the Foto category, it already exists, how can I send a message to the admin panel?
Something like this:

def save(self, *args, **kwargs):
        if Category.objects.filter(self.slug):
            return u'Такая категория уже есть'

You can just kick in the right direction, not necessarily the code.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Scherbakov, 2016-05-13
@Animkim

from django.contrib import messages

class MyAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if blablabla:
            messages.add_message('Category already exists.', level=messages.INFO)
        else:
            return super(MyAdmin, self).save_model(request, obj, form, change)

But it seems to me that your case is more like a form validation, in the sense that it highlights the field where the user made a mistake, say the category name, and does not allow saving. This is another question, it is solved, for example, by setting an attribute on unique=Truethe category name field.

O
Oscar Django, 2016-05-13
@winordie

djbook.ru/rel1.9/ref/contrib/admin/index.html#djan...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question