Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[0.5.X] Fixed #9462 -- Set the instance in an inline formset correctl…

…y so that None does not get passed through to the queryset. Thanks tobias and copelco for the ticket.

Backport of r9293 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9294 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 38d9b0919bb07f4dcfcf22a9013b1a711f54ab89 1 parent 84216ef
Brian Rosner authored October 28, 2008
5  django/forms/models.py
@@ -446,7 +446,10 @@ class BaseInlineFormSet(BaseModelFormSet):
446 446
     def __init__(self, data=None, files=None, instance=None,
447 447
                  save_as_new=False, prefix=None):
448 448
         from django.db.models.fields.related import RelatedObject
449  
-        self.instance = instance
  449
+        if instance is None:
  450
+            self.instance = self.model()
  451
+        else:
  452
+            self.instance = instance
450 453
         self.save_as_new = save_as_new
451 454
         # is there a better way to get the object descriptor?
452 455
         self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
26  tests/modeltests/model_formsets/models.py
@@ -107,6 +107,17 @@ class Membership(models.Model):
107 107
     date_joined = models.DateTimeField(default=datetime.datetime.now)
108 108
     karma = models.IntegerField()
109 109
 
  110
+# models for testing a null=True fk to a parent
  111
+class Team(models.Model):
  112
+    name = models.CharField(max_length=100)
  113
+
  114
+class Player(models.Model):
  115
+    team = models.ForeignKey(Team, null=True)
  116
+    name = models.CharField(max_length=100)
  117
+    
  118
+    def __unicode__(self):
  119
+        return self.name
  120
+
110 121
 __test__ = {'API_TESTS': """
111 122
 
112 123
 >>> from datetime import date
@@ -701,4 +712,19 @@ class Membership(models.Model):
701 712
 >>> formset.is_valid()
702 713
 True
703 714
 
  715
+# inlineformset_factory tests with fk having null=True. see #9462.
  716
+# create some data that will exbit the issue
  717
+>>> team = Team.objects.create(name=u"Red Vipers")
  718
+>>> Player(name="Timmy").save()
  719
+>>> Player(name="Bobby", team=team).save()
  720
+
  721
+>>> PlayerInlineFormSet = inlineformset_factory(Team, Player)
  722
+>>> formset = PlayerInlineFormSet()
  723
+>>> formset.get_queryset()
  724
+[]
  725
+
  726
+>>> formset = PlayerInlineFormSet(instance=team)
  727
+>>> formset.get_queryset()
  728
+[<Player: Bobby>]
  729
+
704 730
 """}

0 notes on commit 38d9b09

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