Fixed #9462 -- Set the instance in an inline formset correctly so that None does not get passed through to the queryset. Thanks tobias and copelco for the ticket.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9293 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2008-10-28 20:01:03 +00:00
parent 2026934600
commit 524a44e20b
2 changed files with 30 additions and 1 deletions

View File

@ -446,7 +446,10 @@ class BaseInlineFormSet(BaseModelFormSet):
def __init__(self, data=None, files=None, instance=None,
save_as_new=False, prefix=None):
from django.db.models.fields.related import RelatedObject
self.instance = instance
if instance is None:
self.instance = self.model()
else:
self.instance = instance
self.save_as_new = save_as_new
# is there a better way to get the object descriptor?
self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()

View File

@ -107,6 +107,17 @@ class Membership(models.Model):
date_joined = models.DateTimeField(default=datetime.datetime.now)
karma = models.IntegerField()
# models for testing a null=True fk to a parent
class Team(models.Model):
name = models.CharField(max_length=100)
class Player(models.Model):
team = models.ForeignKey(Team, null=True)
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
__test__ = {'API_TESTS': """
>>> from datetime import date
@ -701,4 +712,19 @@ False
>>> formset.is_valid()
True
# inlineformset_factory tests with fk having null=True. see #9462.
# create some data that will exbit the issue
>>> team = Team.objects.create(name=u"Red Vipers")
>>> Player(name="Timmy").save()
>>> Player(name="Bobby", team=team).save()
>>> PlayerInlineFormSet = inlineformset_factory(Team, Player)
>>> formset = PlayerInlineFormSet()
>>> formset.get_queryset()
[]
>>> formset = PlayerInlineFormSet(instance=team)
>>> formset.get_queryset()
[<Player: Bobby>]
"""}