Fixed #10893 -- Corrected a problem with using list_editable in the admin when the model has a manual primary key. Thanks to Alex Gaynor for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10681 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2009-05-07 11:56:10 +00:00
parent 95477d7a80
commit eb81d5acb3
3 changed files with 24 additions and 5 deletions

View File

@ -560,10 +560,10 @@ class BaseModelFormSet(BaseFormSet):
# data back. Generally, pk.editable should be false, but for some # data back. Generally, pk.editable should be false, but for some
# reason, auto_created pk fields and AutoField's editable attribute is # reason, auto_created pk fields and AutoField's editable attribute is
# True, so check for that as well. # True, so check for that as well.
def pk_is_editable(pk): def pk_is_not_editable(pk):
return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField)) return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField))
or (pk.rel and pk.rel.parent_link and pk_is_editable(pk.rel.to._meta.pk))) or (pk.rel and pk.rel.parent_link and pk_is_not_editable(pk.rel.to._meta.pk)))
if pk_is_editable(pk): if pk_is_not_editable(pk) or pk.name not in form.fields:
try: try:
pk_value = self.get_queryset()[index].pk pk_value = self.get_queryset()[index].pk
except IndexError: except IndexError:

View File

@ -325,6 +325,20 @@ class GalleryAdmin(admin.ModelAdmin):
class PictureAdmin(admin.ModelAdmin): class PictureAdmin(admin.ModelAdmin):
pass pass
class Language(models.Model):
iso = models.CharField(max_length=5, primary_key=True)
name = models.CharField(max_length=50)
english_name = models.CharField(max_length=50)
shortlist = models.BooleanField(default=False)
class Meta:
ordering = ('iso',)
class LanguageAdmin(admin.ModelAdmin):
list_display = ['iso', 'shortlist', 'english_name', 'name']
list_editable = ['shortlist']
admin.site.register(Article, ArticleAdmin) admin.site.register(Article, ArticleAdmin)
admin.site.register(CustomArticle, CustomArticleAdmin) admin.site.register(CustomArticle, CustomArticleAdmin)
admin.site.register(Section, save_as=True, inlines=[ArticleInline]) admin.site.register(Section, save_as=True, inlines=[ArticleInline])
@ -343,6 +357,7 @@ admin.site.register(EmptyModel, EmptyModelAdmin)
admin.site.register(Fabric, FabricAdmin) admin.site.register(Fabric, FabricAdmin)
admin.site.register(Gallery, GalleryAdmin) admin.site.register(Gallery, GalleryAdmin)
admin.site.register(Picture, PictureAdmin) admin.site.register(Picture, PictureAdmin)
admin.site.register(Language, LanguageAdmin)
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
# That way we cover all four cases: # That way we cover all four cases:

View File

@ -16,7 +16,7 @@ from django.utils.html import escape
from models import (Article, BarAccount, CustomArticle, EmptyModel, from models import (Article, BarAccount, CustomArticle, EmptyModel,
ExternalSubscriber, FooAccount, Gallery, ExternalSubscriber, FooAccount, Gallery,
ModelWithStringPrimaryKey, Person, Persona, Picture, ModelWithStringPrimaryKey, Person, Persona, Picture,
Podcast, Section, Subscriber, Vodcast) Podcast, Section, Subscriber, Vodcast, Language)
try: try:
set set
@ -824,6 +824,11 @@ class AdminViewListEditable(TestCase):
response = self.client.get('/test_admin/admin/admin_views/vodcast/') response = self.client.get('/test_admin/admin/admin_views/vodcast/')
self.failUnlessEqual(response.status_code, 200) self.failUnlessEqual(response.status_code, 200)
def test_custom_pk(self):
Language.objects.create(iso='en', name='English', english_name='English')
response = self.client.get('/test_admin/admin/admin_views/language/')
self.failUnlessEqual(response.status_code, 200)
def test_changelist_input_html(self): def test_changelist_input_html(self):
response = self.client.get('/test_admin/admin/admin_views/person/') response = self.client.get('/test_admin/admin/admin_views/person/')
# 2 inputs per object(the field and the hidden id field) = 6 # 2 inputs per object(the field and the hidden id field) = 6
@ -1132,4 +1137,3 @@ class AdminInlineFileUploadTest(TestCase):
} }
response = self.client.post('/test_admin/%s/admin_views/gallery/1/' % self.urlbit, post_data) response = self.client.post('/test_admin/%s/admin_views/gallery/1/' % self.urlbit, post_data)
self.failUnless(response._container[0].find("Currently:") > -1) self.failUnless(response._container[0].find("Currently:") > -1)