|
1 from django.test import TestCase |
|
2 from django.contrib.admin.options import ModelAdmin |
|
3 |
|
4 from models import Band |
|
5 |
|
6 class TestAdminOrdering(TestCase): |
|
7 """ |
|
8 Let's make sure that ModelAdmin.queryset uses the ordering we define in |
|
9 ModelAdmin rather that ordering defined in the model's inner Meta |
|
10 class. |
|
11 """ |
|
12 |
|
13 def setUp(self): |
|
14 b1 = Band(name='Aerosmith', bio='', rank=3) |
|
15 b1.save() |
|
16 b2 = Band(name='Radiohead', bio='', rank=1) |
|
17 b2.save() |
|
18 b3 = Band(name='Van Halen', bio='', rank=2) |
|
19 b3.save() |
|
20 |
|
21 def test_default_ordering(self): |
|
22 """ |
|
23 The default ordering should be by name, as specified in the inner Meta |
|
24 class. |
|
25 """ |
|
26 ma = ModelAdmin(Band, None) |
|
27 names = [b.name for b in ma.queryset(None)] |
|
28 self.assertEqual([u'Aerosmith', u'Radiohead', u'Van Halen'], names) |
|
29 |
|
30 def test_specified_ordering(self): |
|
31 """ |
|
32 Let's use a custom ModelAdmin that changes the ordering, and make sure |
|
33 it actually changes. |
|
34 """ |
|
35 class BandAdmin(ModelAdmin): |
|
36 ordering = ('rank',) # default ordering is ('name',) |
|
37 ma = BandAdmin(Band, None) |
|
38 names = [b.name for b in ma.queryset(None)] |
|
39 self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names) |