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