Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19432 closed Cleanup/optimization (fixed)

Using get_object_or_404 on a class that isn't a Django model could result in a more obvious error message.

Reported by: Kit Sunde Owned by: BHold
Component: Core (Other) Version: 1.4
Severity: Normal Keywords:
Cc: BHold Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I have a Calendar model in my app but I wrongly imported calendar.Calendar from python instead which resulted in:

type object 'Calendar' has no attribute '_default_manager'

Which led me to think it was this issue: https://code.djangoproject.com/ticket/10405 perhaps it would be possible to raise a ValueError if get_object_or_404 receives something that is not of the correct type with "type object 'Calendar' is not a Django Model".

In _get_queryset it seems like it should raise a ValueErorr if it is not a Model, Manager, or QuerySet if that is what it expects, rather than to assume it is a Model if it's not the other two as it is now.

Or perhaps it was just PEBCAK on my part. :)

Change History (7)

comment:1 by atykhonov, 12 years ago

I'm sorry, I'm not sure, but I think that this is not an issue...
That error "type object 'Calendar' has no attribute '_default_manager'" obviously makes me think that passed object is of wrong type....

comment:2 by Claude Paroz, 12 years ago

Component: UncategorizedCore (Other)
Easy pickings: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

It wouldn't cost much to wrap the klass._default_manager line into a try... except clause.

comment:3 by BHold, 12 years ago

Owner: changed from nobody to BHold

comment:4 by BHold, 12 years ago

Cc: BHold added
Has patch: set

Submitted pull request -- https://github.com/django/django/pull/581

Passes all tests.

comment:5 by Claude Paroz, 12 years ago

Thanks! I think you only missed the case where a non-model class name is passed to get_object_or_404. I will just add that and commit the patch.

comment:6 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In 6ed6a18a033ba78de9418f5b56b80f8b3d3aaf11:

Fixed #19432 -- Provided better error message for get_object_or_404

Thanks Kit Sunde for the report and Brian Holdefehr for the initial
patch.

comment:7 by Claude Paroz <claude@…>, 12 years ago

In c20c3a20a313b24df6d115e585c04c8dc60d3970:

[1.5.x] Fixed #19432 -- Provided better error message for get_object_or_404

Thanks Kit Sunde for the report and Brian Holdefehr for the initial
patch.
Backport of 6ed6a18a0 from master.

Note: See TracTickets for help on using tickets.
Back to Top