#9462 closed (fixed)
inlineformsets without instances show objects with null foreign keys
Reported by: | Colin Copeland | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.0 |
Severity: | Keywords: | ||
Cc: | Tobias McNulty | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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" />
Change History (6)
comment:1 by , 16 years ago
Cc: | added |
---|
comment:2 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:3 by , 16 years ago
comment:4 by , 16 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
I think you want:
self.fk.rel.to()
instead of
self.model()
comment:5 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
This ticket has been closed with a commit and passing tests, if you think there's another issue please file a new ticket.
Note:
See TracTickets
for help on using tickets.
(In [9293]) 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.