Code

Opened 8 years ago

Closed 7 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
Component: Core (Other) Version: master
Severity: normal Keywords:
Cc: gary.wilson@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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@…> 8 years ago.
shortcuts2.diff (3.0 KB) - added by Gary Wilson <gary.wilson@…> 7 years ago.
added modeltests for get_object_or_404
shortcuts3.diff (3.4 KB) - added by Gary Wilson <gary.wilson@…> 7 years ago.
added a test for custom manager

Download all attachments as: .zip

Change History (4)

Changed 8 years ago by Gary Wilson <gary.wilson@…>

Changed 7 years ago by Gary Wilson <gary.wilson@…>

added modeltests for get_object_or_404

Changed 7 years ago by Gary Wilson <gary.wilson@…>

added a test for custom manager

comment:1 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.