Tuesday, September 27, 2011

model.clean() return errors with field highliting

Hi I'm developing django application that have multiple admin panels.
One for Admins one for customers one for our sales team.

So I figured out to not override code too much best is to make
validation on model level and that's possible with django 1.2.

I needed to check if postal town of contact is correct one when
contact details were in country Ireland. I found that overriding
Model.clean() method I can do those checks and it works nice except it
does not highlight postal town field in form in admin panel.
After digging in and out I found that I could remedy it with this
patch. Is it correct way to do?

--- a/django/forms/models.py
+++ bjango/forms/models.py
@@ -330,7 +330,10 @@ class BaseModelForm(BaseForm):
try:
self.instance.clean()
except ValidationError, e:
- self._update_errors({NON_FIELD_ERRORS: e.messages})
+ if hasattr(e, 'message_dict'):
+ self._update_errors(e.message_dict)
+ else:
+ self._update_errors({NON_FIELD_ERRORS: e.messages})

# Validate uniqueness if needed.
if self._validate_unique:

Then in clean method if I do :
raise ValidationError("wow some error")
I get non field error. And with:
raise ValidationError({"postal_town": ["Enter valid postal
town"]})
it nicely highlights postal town
BTW some minor magic in ValidationError would solve this probably.
Like having ValidationError.form_dict() method that always returns
dict with errors. I just do not know Django that much to make such
changes.

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate