#9462: inlineformsets without instances show objects with null foreign keys
---------------------+------------------------------------------------------
Reporter: copelco | Owner: nobody
Status: new | Milestone:
Component: Forms | Version: 1.0
Keywords: | Stage: Unreviewed
Has_patch: 0 |
---------------------+------------------------------------------------------
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" />
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/9462>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---