Monday, June 27, 2011

Info and warning messages in admin

I need to be able to distinguish between info and warning messages that get passed to the user in admin.

I do this, and it works:

# import the admin messages framework
from django.contrib import admin, messages

class SomeForm(forms.ModelForm):
...
def clean(self):
# create a pair of empty lists for warning and information messages
SomeForm.warnings = []
SomeForm.info = []
... # do some things that add messages to the lists, based on
# things in cleaned_data
return self.cleaned_data

class SomeAdmin(admin.ModelAdmin):
...
def save_model(self, request, obj, form, change):
# loop over the lists of messages, and pass them to the messages system
for message in self.form.warnings:
messages.warning(request, message)
for message in self.form.info:
messages.info(request, message)
return super(SomeAdmin, self).save_model(request, obj, form, change)

This way yellow info messges get a little tick icon, while warnings have a warning sign (and really, ought to come up in orange, not yellow).

Is this a good or correct way of doing it?

Thanks for any advice or suggestions.

Daniele


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment