Hi,
I'm using following piece of code with ExtJS:
<code>
def extjs_validate_instance(instance):
"""Validate given Django model instance.
Return ExtJS formatted error response.
"""
try:
instance.full_clean() # Validate
except ValidationError, e:
opts = instance._meta
nice_messages = []
for fname, msgs in e.message_dict.items():
nice_messages.append({
'name' :
force_unicode(opts.get_field_by_name(fname)[0].verbose_name),
'error' : ', '.join(msgs),
})
response_obj = {
'success': False,
'items' : nice_messages}
raise ErrorResponse(status.HTTP_200_OK, response_obj)
</code>
Now it returns dict "nice_messages" which contains fieldname and
message(s) associated with that field.
13.9.2012 2:36, Kurtis Mullins kirjoitti:
Just a quick example of a field that can have two completely different
error types but both throw a ValidationError while running full_clean().
Let's say my Model includes an email address. The email address must be
unique. It could either fail because 1. It's not Unique (or) 2. It's an
invalid email address.
What I really want to do is determine the exception/error-type so that I
can handle it appropriately.
On Wed, Sep 12, 2012 at 7:09 PM, Kurtis <kurtis.mull...@gmail.com
<mailto:kurtis.mull...@gmail.com>> wrote:
Hey Guys,
Do you have any suggestions on a good way to grab error types during
Model Validation? I'm not using Forms or HTML. First, here's a
simplified snippet showing my current format for Model Data Validation.
my_object = MyModel(**data_dict)
try:
my_object.full_clean()
my_object.save()
return SuccessJSONResponse()
except ValidationError, e:
print e.message_dict # Testing
# ... build some custom JSON data from the error
return ErrorJSONResponse(json_data)
This works fine for properly validating the data and being able to
print out errors. It also allows me to easily see which fields threw
errors. Unfortunately, it doesn't provide me with the types of
errors in a "programmatic" fashion as far as I can tell. For
example, a field can fail because it's a duplicate entry or it can
fail because it didn't meet the criteria of the field-type (e.g.
max_length).
What I'm hoping for is there's a way I can still easily validate my
data using my Model but get more specific information about the
types of errors. If it's an IntegrityError then I need to build a
custom response indicating which field had failed along with an
"internal" (to the project) error code and a description. If it's
some other specific error (for example, maybe an email address isn't
a "valid" email address) then I'd like to identify the error type,
field name, and display that error accordingly. Hopefully that makes
sense :)
I'm not sure if I'm just over-looking something or trying to do
things the hard way but I'm up for suggestions. Keep in mind that
Forms + HTML are out of the question since this is an API-only
application.
Thanks!
- Kurtis
--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/7CS8sJxLHhwJ.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@googlegroups.com>.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com
<mailto:django-users%2bunsubscr...@googlegroups.com>.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
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.
--
Jani Tiainen
- Well planned is half done and a half done has been sufficient before...
--
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.