Ticket #15032: 15032-1.2.X.diff

File 15032-1.2.X.diff, 3.7 KB (added by Ramiro Morales, 14 years ago)

Fix plus tests for 1.2.X branch

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    a b  
    195195
    196196        # Special case -- foo__id__exact and foo__id queries are implied
    197197        # if foo has been specificially included in the lookup list; so
    198         # drop __id if it is the last part.
    199         if len(parts) > 1 and parts[-1] == self.model._meta.pk.name:
     198        # drop __id if it is the last part. However, first we need to find
     199        # the pk attribute name.
     200        model = self.model
     201        pk_attr_name = None
     202        for part in parts[:-1]:
     203            field, _, _, _ = model._meta.get_field_by_name(part)
     204            if hasattr(field, 'rel'):
     205                model = field.rel.to
     206                pk_attr_name = model._meta.pk.name
     207            elif isinstance(field, RelatedObject):
     208                model = field.model
     209                pk_attr_name = model._meta.pk.name
     210            else:
     211                pk_attr_name = None
     212        if pk_attr_name and len(parts) > 1 and parts[-1] == pk_attr_name:
    200213            parts.pop()
    201214
    202215        try:
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    a b  
    588588class AlbumAdmin(admin.ModelAdmin):
    589589    list_filter = ['title']
    590590
     591class Employee(Person):
     592    code = models.CharField(max_length=20)
     593
     594class WorkHour(models.Model):
     595    datum = models.DateField()
     596    employee = models.ForeignKey(Employee)
     597
     598class WorkHourAdmin(admin.ModelAdmin):
     599    list_display = ('datum', 'employee')
     600    list_filter = ('employee',)
     601
    591602admin.site.register(Article, ArticleAdmin)
    592603admin.site.register(CustomArticle, CustomArticleAdmin)
    593604admin.site.register(Section, save_as=True, inlines=[ArticleInline])
     
    619630admin.site.register(PlotDetails)
    620631admin.site.register(CyclicOne)
    621632admin.site.register(CyclicTwo)
     633admin.site.register(WorkHour, WorkHourAdmin)
    622634
    623635# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    624636# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    a b  
    2828    FooAccount, Gallery, ModelWithStringPrimaryKey, \
    2929    Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
    3030    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \
    31     Category, Post, Plot, FunkyTag
     31    Category, Post, Plot, FunkyTag, WorkHour, Employee
    3232
    3333
    3434class AdminViewBasicTest(TestCase):
     
    311311        except SuspiciousOperation:
    312312            self.fail("Filters should be allowed if they involve a local field without the need to whitelist them in list_filter or date_hierarchy.")
    313313
     314        e1 = Employee.objects.create(name='Anonymous', gender=1, age=22, alive=True, code='123')
     315        e2 = Employee.objects.create(name='Visitor', gender=2, age=19, alive=True, code='124')
     316        WorkHour.objects.create(datum=datetime.datetime.now(), employee=e1)
     317        WorkHour.objects.create(datum=datetime.datetime.now(), employee=e2)
     318        response = self.client.get("/test_admin/admin/admin_views/workhour/")
     319        self.assertEqual(response.status_code, 200)
     320        self.assertContains(response, 'employee__person_ptr__exact')
     321        response = self.client.get("/test_admin/admin/admin_views/workhour/?employee__person_ptr__exact=%d" % e1.pk)
     322        self.assertEqual(response.status_code, 200)
     323
    314324class SaveAsTests(TestCase):
    315325    fixtures = ['admin-views-users.xml','admin-views-person.xml']
    316326
Back to Top