On Tue, Aug 18, 2009 at 4:45 PM, Marek Palatinus<ma...@palatinus.cz> wrote:
> On Tue, Aug 18, 2009 at 4:36 PM, Joshua Russo<josh.r.ru...@gmail.com> wrote:
>> On Tue, Aug 18, 2009 at 1:23 PM, Marek Palatinus <ma...@palatinus.cz> wrote:
>>>
>>> On Tue, Aug 18, 2009 at 12:22 PM, Joshua Russo<josh.r.ru...@gmail.com>
>>> wrote:
>>> > On Tue, Aug 18, 2009 at 8:52 AM, Marek Palatinus <ma...@palatinus.cz>
>>> > wrote:
>>> >>
>>> >> Hello,
>>> >>
>>> >> Im doing some validation in ModelAdmin.save_model(). I can cancel
>>> >> operation (just dont call parent method), but I also need to show some
>>> >> error message to user. Im able to call self.message_user() and show
>>> >> INFO message, but how to send ERROR message and maybe show form back
>>> >> and force user to correct inputs?
>>> >>
>>> >> I need validation on level of whole formset, not on field level, so I
>>> >> cannot use some custom field to achieve that.
>>> >
>>> > What you want is to override the ModelAdmin.form with a custom form and
>>> > handle the clean_xxx() methods there. Django form validation is done in
>>> > the
>>> > form objects/classes.
>>> > Create a ModelForm
>>> > (http://docs.djangoproject.com/en/dev/topics/forms/modelforms/) and then
>>> > set
>>> > your ModelAdmin.form to your new ModelForm.
>>> >
>>>
>>> Joshua, thanks a lot!
>>>
>>> to others: there also exists clean() which have available all fields
>>> from form at one place.

Im sending working code for others:

class SlugModelForm(ModelForm):
    def clean_slug(self, *args):
        if not self.instance.allow_slug_slashes and
self.data['slug'].find('/') != -1:
            msg = _("Slash '/' is not allowed here.")
            self.errors['slug'] = ErrorList([msg])
        return self.data['slug']

class BaseModuleAdmin(admin.ModelAdmin):
    list_editable = ('enabled',)
    exclude = ('site', )
    form = SlugModelForm
     ....
Im using slug as universal identifier across tens modules inside
application, but in some rare cases I want to allow slashes inside
slugs (=full urls). Construction above is logic for showing error
message to user, when he write slash to module's slug,  which have
disabled this feature.

Regards,
Marek

--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to