From d8fdee7db877e3a52a761bfcbeb3536ea219ec30 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 20 Nov 2013 21:34:29 +0100 Subject: [PATCH] [1.6.x] Fixed #21472 -- Fixed inline formsets display when parent pk is 0 Thanks agale031176@gmail.com for the report. Backport of fafb6cf049b from master. --- django/forms/models.py | 2 +- docs/releases/1.6.1.txt | 2 ++ tests/inline_formsets/tests.py | 11 ++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index 67f8778c46..5c4fe4bbdf 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -831,7 +831,7 @@ class BaseInlineFormSet(BaseModelFormSet): self.save_as_new = save_as_new if queryset is None: queryset = self.model._default_manager - if self.instance.pk: + if self.instance.pk is not None: qs = queryset.filter(**{self.fk.name: self.instance}) else: qs = queryset.none() diff --git a/docs/releases/1.6.1.txt b/docs/releases/1.6.1.txt index d88b404bc5..d7f73903ff 100644 --- a/docs/releases/1.6.1.txt +++ b/docs/releases/1.6.1.txt @@ -26,3 +26,5 @@ Bug fixes for the non-breaking space was returned verbatim (#21415). * Fixed :djadmin:`loaddata` error when fixture file name contained any dots non related to file extensions (#21457). +* Fixed display of inline instances in formsets when parent has 0 for primary + key (#21472). diff --git a/tests/inline_formsets/tests.py b/tests/inline_formsets/tests.py index ad8a666cb5..d2f79ae643 100644 --- a/tests/inline_formsets/tests.py +++ b/tests/inline_formsets/tests.py @@ -1,7 +1,7 @@ from __future__ import absolute_import, unicode_literals from django.forms.models import inlineformset_factory -from django.test import TestCase +from django.test import TestCase, skipUnlessDBFeature from django.utils import six from .models import Poet, Poem, School, Parent, Child @@ -157,3 +157,12 @@ class InlineFormsetFactoryTest(TestCase): inlineformset_factory( Parent, Child, exclude=('school',), fk_name='mother' ) + + @skipUnlessDBFeature('allows_primary_key_0') + def test_zero_primary_key(self): + # Regression test for #21472 + poet = Poet.objects.create(id=0, name='test') + poem = poet.poem_set.create(name='test poem') + PoemFormSet = inlineformset_factory(Poet, Poem, fields="__all__", extra=0) + formset = PoemFormSet(None, instance=poet) + self.assertEqual(len(formset.forms), 1)