#19364: Wrong exception raised at django/db/models/fields/related.py:343? (DoesNotExist instead of AttributeError) -------------------------------------+------------------------------------- Reporter: lbragues@… | Owner: lbragues Type: Bug | Status: new Component: Documentation | Version: 1.4 Severity: Normal | Resolution: Keywords: DoesNotExist, | Triage Stage: Accepted django.db, getattr, | Needs documentation: 0 Has patch: 1 | Patch needs improvement: 0 Needs tests: 0 | UI/UX: 0 Easy pickings: 0 | -------------------------------------+-------------------------------------
Comment (by lbragues): Then by throwing DoesNotExist we can't simply do a getattr call to check if the attribute exists in the db and if it has a value assigned, since DoesNotExists isn't catched by getattr. It goes a little bit against what you expect python to do from reading the python docs http://docs.python.org/3/library/functions.html#getattr (docs v2 is equal). And also if you do a dir(obj) the output shows that the object as a property with the name you are trying to get, so it can be a little confusing at first... So the solution I adopted to do this is to try...except: {{{ try: #do something with object except DosNotExist: #do something on exception }}} -- Ticket URL: <https://code.djangoproject.com/ticket/19364#comment:4> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.