Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10799: fixed the use of list_editable with model inheritance a…

…nd custom one-to-one parent links. Thanks, Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10590 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ccc8e104eef01752f078bedc99ca6b756e1f048c 1 parent 1e4ad6f
Jacob Kaplan-Moss authored April 18, 2009
5  django/forms/models.py
@@ -470,7 +470,10 @@ def add_fields(self, form, index):
470 470
         # data back. Generally, pk.editable should be false, but for some
471 471
         # reason, auto_created pk fields and AutoField's editable attribute is
472 472
         # True, so check for that as well.
473  
-        if (not pk.editable) or (pk.auto_created or isinstance(pk, AutoField)):
  473
+        def pk_is_editable(pk):
  474
+            return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField))
  475
+                or (pk.rel and pk.rel.parent_link and pk_is_editable(pk.rel.to._meta.pk)))
  476
+        if pk_is_editable(pk):
474 477
             try:
475 478
                 pk_value = self.get_queryset()[index].pk
476 479
             except IndexError:
9  tests/modeltests/model_formsets/models.py
@@ -108,6 +108,10 @@ class Meta:
108 108
 class MexicanRestaurant(Restaurant):
109 109
     serves_tacos = models.BooleanField()
110 110
 
  111
+class ClassyMexicanRestaurant(MexicanRestaurant):
  112
+    restaurant = models.OneToOneField(MexicanRestaurant, parent_link=True, primary_key=True)
  113
+    tacos_are_yummy = models.BooleanField()
  114
+
111 115
 # models for testing unique_together validation when a fk is involved and
112 116
 # using inlineformset_factory.
113 117
 class Repository(models.Model):
@@ -934,4 +938,9 @@ def __unicode__(self):
934 938
 >>> formset.get_queryset()
935 939
 [<Player: Bobby>]
936 940
 
  941
+# a formset for a Model that has a custom primary key that still needs to be
  942
+# added to the formset automatically
  943
+>>> FormSet = modelformset_factory(ClassyMexicanRestaurant, fields=["tacos_are_yummy"])
  944
+>>> sorted(FormSet().forms[0].fields.keys())
  945
+['restaurant', 'tacos_are_yummy']
937 946
 """}
11  tests/regressiontests/admin_views/models.py
@@ -269,6 +269,16 @@ class PodcastAdmin(admin.ModelAdmin):
269 269
 
270 270
     ordering = ('name',)
271 271
 
  272
+class Vodcast(Media):
  273
+    media = models.OneToOneField(Media, primary_key=True, parent_link=True)
  274
+    released = models.BooleanField(default=False)
  275
+
  276
+class VodcastAdmin(admin.ModelAdmin):
  277
+    list_display = ('name', 'released')
  278
+    list_editable = ('released',)
  279
+
  280
+    ordering = ('name',)
  281
+
272 282
 class Parent(models.Model):
273 283
     name = models.CharField(max_length=128)
274 284
 
@@ -327,6 +337,7 @@ class PictureAdmin(admin.ModelAdmin):
327 337
 admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
328 338
 admin.site.register(OldSubscriber, OldSubscriberAdmin)
329 339
 admin.site.register(Podcast, PodcastAdmin)
  340
+admin.site.register(Vodcast, VodcastAdmin)
330 341
 admin.site.register(Parent, ParentAdmin)
331 342
 admin.site.register(EmptyModel, EmptyModelAdmin)
332 343
 admin.site.register(Fabric, FabricAdmin)
9  tests/regressiontests/admin_views/tests.py
@@ -2,8 +2,6 @@
2 2
 
3 3
 import re
4 4
 import datetime
5  
-import os
6  
-
7 5
 from django.core.files import temp as tempfile
8 6
 from django.test import TestCase
9 7
 from django.contrib.auth.models import User, Permission
@@ -18,7 +16,7 @@
18 16
 from models import (Article, BarAccount, CustomArticle, EmptyModel,
19 17
                     ExternalSubscriber, FooAccount, Gallery,
20 18
                     ModelWithStringPrimaryKey, Person, Persona, Picture,
21  
-                    Podcast, Section, Subscriber)
  19
+                    Podcast, Section, Subscriber, Vodcast)
22 20
 
23 21
 try:
24 22
     set
@@ -801,6 +799,11 @@ def test_inheritance(self):
801 799
         response = self.client.get('/test_admin/admin/admin_views/podcast/')
802 800
         self.failUnlessEqual(response.status_code, 200)
803 801
 
  802
+    def test_inheritance_2(self):
  803
+        Vodcast.objects.create(name="This Week in Django", released=True)
  804
+        response = self.client.get('/test_admin/admin/admin_views/vodcast/')
  805
+        self.failUnlessEqual(response.status_code, 200)
  806
+
804 807
     def test_changelist_input_html(self):
805 808
         response = self.client.get('/test_admin/admin/admin_views/person/')
806 809
         # 2 inputs per object(the field and the hidden id field) = 6

0 notes on commit ccc8e10

Please sign in to comment.
Something went wrong with that request. Please try again.