﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
9462	inlineformsets without instances show objects with null foreign keys	Colin Copeland	nobody	"Models:
{{{
class Team(models.Model):
    name = models.CharField(max_length=255)
    
class Player(models.Model):
    name = models.CharField(max_length=255)
    team = models.ForeignKey(Team, null=True)
    
    def __unicode__(self):
        return self.name
}}}

Failure without passing instance into !PlayerFormSet:

{{{
In [1]: from django import forms
In [2]: from django.forms.models import inlineformset_factory
In [3]: from baseball.models import Team, Player
In [4]: 
In [5]: class TeamForm(forms.ModelForm):
   ...:     class Meta:
   ...:         model = Team
   ...: 
In [6]: PlayerFormSet = inlineformset_factory(Team, Player, extra=0)
In [7]: print 'Players not on a team:', Player.objects.filter(team__isnull=True) 
Players not on a team: [<Player: John Doe>]
In [8]: print PlayerFormSet()
<input type=""hidden"" name=""player_set-TOTAL_FORMS"" value=""1"" id=""id_player_set-TOTAL_FORMS"" /><input type=""hidden"" name=""player_set-INITIAL_FORMS"" value=""1"" id=""id_player_set-INITIAL_FORMS"" />
<tr><th><label for=""id_player_set-0-name"">Name:</label></th><td><input id=""id_player_set-0-name"" type=""text"" name=""player_set-0-name"" value=""John Doe"" maxlength=""255"" /></td></tr>
<tr><th><label for=""id_player_set-0-DELETE"">Delete:</label></th><td><input type=""checkbox"" name=""player_set-0-DELETE"" id=""id_player_set-0-DELETE"" /><input type=""hidden"" name=""player_set-0-id"" value=""1"" id=""id_player_set-0-id"" /></td></tr>
}}}

Works with empty instance object:

{{{
In [1]: from django import forms
In [2]: from django.forms.models import inlineformset_factory
In [3]: from baseball.models import Team, Player
In [4]: 
In [5]: class TeamForm(forms.ModelForm):
   ...:     class Meta:
   ...:         model = Team
   ...: 
In [6]: PlayerFormSet = inlineformset_factory(Team, Player, extra=0)
In [7]: print 'Players not on a team:', Player.objects.filter(team__isnull=True) 
Players not on a team: [<Player: John Doe>]
In [8]: print PlayerFormSet(instance=Team())
<input type=""hidden"" name=""player_set-TOTAL_FORMS"" value=""0"" id=""id_player_set-TOTAL_FORMS"" /><input type=""hidden"" name=""player_set-INITIAL_FORMS"" value=""0"" id=""id_player_set-INITIAL_FORMS"" />
}}}"		closed	Forms	1.0		fixed		Tobias McNulty	Unreviewed	0	0	0	0	0	0
