Opened 18 years ago

Closed 17 years ago

#2756 closed enhancement (fixed)

[patch] allow get_object_or_404 and get_list_or_404 shortcuts to be passed Manager objects

Reported by: Gary Wilson <gary.wilson@…> Owned by: Adrian Holovaty
Component: Core (Other) Version: dev
Severity: normal Keywords:
Cc: gary.wilson@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

By allowing get_object_or_404 and get_list_or_404 to be passed Manager instances, you can use the shortcut with related models. For example with the models:

class Author(models.Model):
    name = models.CharField(maxlength=50)

class Article(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(maxlength=50)

you could do something like:

author = Author.objects.get(name="Brave Sir Robin")
article = get_object_or_404(author.article_set, title="Run away!")
articles = get_list_or_404(author.article_set, title__contains="Camelot")

Attachments (3)

shortcuts.diff (1.2 KB ) - added by Gary Wilson <gary.wilson@…> 18 years ago.
shortcuts2.diff (3.0 KB ) - added by Gary Wilson <gary.wilson@…> 17 years ago.
added modeltests for get_object_or_404
shortcuts3.diff (3.4 KB ) - added by Gary Wilson <gary.wilson@…> 17 years ago.
added a test for custom manager

Download all attachments as: .zip

Change History (4)

by Gary Wilson <gary.wilson@…>, 18 years ago

Attachment: shortcuts.diff added

by Gary Wilson <gary.wilson@…>, 17 years ago

Attachment: shortcuts2.diff added

added modeltests for get_object_or_404

by Gary Wilson <gary.wilson@…>, 17 years ago

Attachment: shortcuts3.diff added

added a test for custom manager

comment:1 by Russell Keith-Magee, 17 years ago

Resolution: fixed
Status: newclosed

(In [4275]) Fixed #2756 -- Modified the get_object_or_404/get_list_or_404 shortcuts to accept model managers as well as model classes. Thanks, Gary Wilson.

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