Opened 13 years ago
Closed 11 years ago
#17582 closed Cleanup/optimization (fixed)
DoesNotExist ambiguous exception when accessed via foreign key
Reported by: | Owned by: | JordanPowell | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | JordanPowell, Simon Charette, timograham@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Hi.
When a Foo.DoesNotExist exception is raised, the debug page only prints 'DoesNotExist', without showing which model it belongs to.
Is there some way we can fix this? I would like to see as much information as possible.
Thanks.
Simon
Attachments (2)
Change History (18)
comment:1 by , 13 years ago
comment:2 by , 13 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Triage Stage: | Unreviewed → Design decision needed |
Yeah, this needs more information. What is the expected output?
comment:3 by , 13 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → reopened |
Summary: | Debug page - ambiguous exception type (should show full path) → DoesNotExist ambiguous exception when accessed via foreign key |
When accessed via a query, this does indeed give the model name in the message (e.g. 'FooBar matching query does not exist').
However, when accessing a foreign key which either has an invalid value or is null, the exception is raised without any message:
.../django/db/models/fields/related.py:301 in __get__ raise self.field.rel.to.DoesNotExist
This causes the debug page just to show 'DoesNotExist' and 'No exception supplied'.
Since Model.DoesNotExist is created dynamically when the model is loaded, surely it would be possible to the name of the model as the default message or similar?
Thanks.
comment:4 by , 12 years ago
Component: | Template system → Database layer (models, ORM) |
---|
comment:5 by , 12 years ago
Cc: | added |
---|
Any idea why this is DDN? I can't see any problem with making the exception more informative. It looks like someone just forgot to instantiate it.
I'm happy to put together a patch and tests for this, if someone agrees that there's no Design Decision Needed.
I'd probably just change the line to:
raise self.field.rel.to.DoesNotExist( "This %s has no %s (the %s is null)" % (self.field.model._meta.object_name, self.field.name, self.field.rel.to._meta.object_name))
so it reads:
This Model has no field (the RelatedModel is null).
Any comments on the wording?
comment:6 by , 12 years ago
Cc: | added |
---|---|
Needs documentation: | set |
Needs tests: | set |
Triage Stage: | Design decision needed → Accepted |
Type: | Bug → Cleanup/optimization |
Version: | 1.3 → master |
Accepting the ticket as an improvement.
raise self.field.rel.to.DoesNotExist( "%s has no %s." % (self.field.model.__name__, self.field.name) )
Model has no field. sounds better to me but I'd also like comments on the wording.
I removed the null
part since this exception can also be raised if the field as an invalid value as pointed out in comment:3.
This needs testing and an addition to the release note.
comment:8 by , 12 years ago
Status: | reopened → new |
---|
comment:9 by , 12 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
by , 12 years ago
Attachment: | #17582.patch added |
---|
comment:10 by , 12 years ago
Just added a patch for this - let me know if anything needs changing and if I should issue a pull request.
comment:11 by , 12 years ago
Patch needs improvement: | set |
---|
Instead of testing for the exception message being not empty you should use assertRaisesMessage
to make sure the correct message is raised.
by , 12 years ago
Attachment: | #17582.2.patch added |
---|
comment:12 by , 12 years ago
Ok cool - how's that? I wasn't sure if I should tie the test so closely to the message, thanks for clarifying.
comment:13 by , 12 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Apart from the missing point in the release note the patch looks RFC. Will commit soon with this minor change.
comment:14 by , 11 years ago
Cc: | added |
---|---|
Has patch: | set |
Needs documentation: | unset |
Needs tests: | unset |
Patch needs improvement: | unset |
Triage Stage: | Ready for checkin → Accepted |
In reviewing this, I found another instance of raise DoesNotExist
without a message. Updated in the following pull request: https://github.com/django/django/pull/1212
(moving back to accepted to get another review)
comment:16 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Below the exception type is the exception value which will usually say something like
Then below that is a full traceback which should point you to which piece of code which made the erroneous query.