Django

Code

Ticket #2756 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: Gary Wilson <gary.wilson@gmail.com> Assigned to: adrian
Milestone: Component: Core framework
Version: SVN Keywords:
Cc: gary.wilson@gmail.com Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

shortcuts.diff (1.2 kB) - added by Gary Wilson <gary.wilson@gmail.com> on 09/18/06 09:14:38.
shortcuts2.diff (3.0 kB) - added by Gary Wilson <gary.wilson@gmail.com> on 12/25/06 00:32:09.
added modeltests for get_object_or_404
shortcuts3.diff (3.4 kB) - added by Gary Wilson <gary.wilson@gmail.com> on 12/27/06 11:07:47.
added a test for custom manager

Change History

09/18/06 09:14:38 changed by Gary Wilson <gary.wilson@gmail.com>

  • attachment shortcuts.diff added.

12/25/06 00:32:09 changed by Gary Wilson <gary.wilson@gmail.com>

  • attachment shortcuts2.diff added.

added modeltests for get_object_or_404

12/27/06 11:07:47 changed by Gary Wilson <gary.wilson@gmail.com>

  • attachment shortcuts3.diff added.

added a test for custom manager

01/03/07 08:16:58 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(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/Change #2756 ([patch] allow get_object_or_404 and get_list_or_404 shortcuts to be passed Manager objects)




Change Properties
Action