#21563: calling hasattr(model_instance, fieldname) raises DoesNotExist when False -------------------------------------+------------------------------------- Reporter: monkut | Owner: nobody Type: | Status: new Cleanup/optimization | Version: 1.6 Component: Database layer | Resolution: (models, ORM) | Triage Stage: Accepted Severity: Normal | Needs documentation: 0 Keywords: | Patch needs improvement: 0 Has patch: 0 | UI/UX: 0 Needs tests: 0 | Easy pickings: 0 | -------------------------------------+------------------------------------- Changes (by aaugustin):
* needs_better_patch: => 0 * component: Uncategorized => Database layer (models, ORM) * needs_tests: => 0 * needs_docs: => 0 * type: Uncategorized => Cleanup/optimization * stage: Unreviewed => Accepted Comment: This is a bit tricky. [http://docs.python.org/3/library/functions.html#hasattr Python's documentation for hasattr] notes that it's "implemented by calling `getattr(object, name)` and seeing whether it raises an `AttributeError` or not". However, when no related object exists, Django raises `DoesNotExist`, not `AttributeError`. Could we raise a `RelatedObjectDoesNotExist` exception inheriting both from `DoesNotExist` (for backwards compatibility) and `AttributeError` (to change the behavior according to your suggestion)? I'm not entirely sure it's a good idea, but it's worth considering. -- Ticket URL: <https://code.djangoproject.com/ticket/21563#comment:1> 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 unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.27fcbc9dbafb88bf7b6cb270114df6b1%40djangoproject.com. For more options, visit https://groups.google.com/groups/opt_out.