Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#9462 closed (fixed)

inlineformsets without instances show objects with null foreign keys

Reported by: copelco Owned by: nobody
Component: Forms Version: 1.0
Severity: Keywords:
Cc: tobias Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 Changed 7 years ago by tobias

  • Cc tobias added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by brosner

  • Resolution set to fixed
  • Status changed from new to closed

(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.

comment:3 Changed 7 years ago by brosner

(In [9294]) [0.5.X] 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.

Backport of r9293 from trunk.

comment:4 Changed 7 years ago by tobias

  • Resolution fixed deleted
  • Status changed from closed to reopened

I think you want:

self.fk.rel.to()

instead of

self.model()

comment:5 Changed 7 years ago by Alex

  • Resolution set to fixed
  • Status changed from reopened to closed

This ticket has been closed with a commit and passing tests, if you think there's another issue please file a new ticket.

comment:6 Changed 6 years ago by tobias

opened #11872

Note: See TracTickets for help on using tickets.
Back to Top