Fixed #13862 -- Added an ordering option to InlineModelAdmin and cleaned up documentation for it a bit. Thanks, Simon Meers, rasca and cogat.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14882 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-12-12 22:56:01 +00:00
parent 1e7bb904df
commit b3520da9ac
4 changed files with 97 additions and 23 deletions

View file

@ -1,5 +1,6 @@
# coding: utf-8
from django.db import models
from django.contrib import admin
class Band(models.Model):
name = models.CharField(max_length=100)
@ -8,3 +9,18 @@ class Band(models.Model):
class Meta:
ordering = ('name',)
class Song(models.Model):
band = models.ForeignKey(Band)
name = models.CharField(max_length=100)
duration = models.IntegerField()
class Meta:
ordering = ('name',)
class SongInlineDefaultOrdering(admin.StackedInline):
model = Song
class SongInlineNewOrdering(admin.StackedInline):
model = Song
ordering = ('duration', )

View file

@ -1,7 +1,7 @@
from django.test import TestCase
from django.contrib.admin.options import ModelAdmin
from models import Band
from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering
class TestAdminOrdering(TestCase):
"""
@ -37,3 +37,37 @@ class TestAdminOrdering(TestCase):
ma = BandAdmin(Band, None)
names = [b.name for b in ma.queryset(None)]
self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names)
class TestInlineModelAdminOrdering(TestCase):
"""
Let's make sure that InlineModelAdmin.queryset uses the ordering we define
in InlineModelAdmin.
"""
def setUp(self):
b = Band(name='Aerosmith', bio='', rank=3)
b.save()
self.b = b
s1 = Song(band=b, name='Pink', duration=235)
s1.save()
s2 = Song(band=b, name='Dude (Looks Like a Lady)', duration=264)
s2.save()
s3 = Song(band=b, name='Jaded', duration=214)
s3.save()
def test_default_ordering(self):
"""
The default ordering should be by name, as specified in the inner Meta
class.
"""
inline = SongInlineDefaultOrdering(self.b, None)
names = [s.name for s in inline.queryset(None)]
self.assertEqual([u'Dude (Looks Like a Lady)', u'Jaded', u'Pink'], names)
def test_specified_ordering(self):
"""
Let's check with ordering set to something different than the default.
"""
inline = SongInlineNewOrdering(self.b, None)
names = [s.name for s in inline.queryset(None)]
self.assertEqual([u'Jaded', u'Pink', u'Dude (Looks Like a Lady)'], names)