PrePopulate formset_factory with data from db

Go To StackoverFlow.com

1

I am trying to make an edit form which should be propulated with existing values. Although I am pre-populate all the fields, but the foreignkey field is giving me a problem.

Here is my forms.py

def make_bowling_form(teamid, fixtureid):
    class BowlingForm(forms.Form):
        player = forms.ModelChoiceField(
            queryset=PlayerShortlist.objects.filter(
                team = teamid,
                fixture = fixtureid
            )
        )
        overs = forms.FloatField(initial=0.0)
        runs = forms.IntegerField(initial=0)
        wickets = forms.IntegerField(initial=0)
    return BowlingForm

and views.py

@login_required
def edit_bowling(request, team_id, fixture_id):
    template = get_template('cricket/edit_bowling.html')
    if Team.objects.get(id=team_id) == Team.objects.get(owner=request.user):
        matchscorecard = InningsCard.objects.get(team = team_id, fixture = fixture_id)
        bowlers = BowlingDetail.objects.filter(innings = matchscorecard)
        bowlingforms = formset_factory(
            make_bowling_form(team_id, fixture_id),
            extra=0,
        )
        bowlingforms = bowlingforms(initial = bowlers.values())
        page_vars=Context({
            'loggedinuser': request.user,
            'bowlers': bowlers,
            'bowlingforms': bowlingforms,
        })
    else:
        error = "You dont have permission to add score for this team"
        page_vars=Context({
            'loggedinuser': request.user,
            'error': error
        })
    crsfcontext = RequestContext(request, page_vars)
    output = template.render(crsfcontext)
    return HttpResponse(output)

It is giving me all the values correctly except the foreign key field. t further investigate i went on and printed the bowlers.value()

[{'runs': 32, 'innings_id': 1, 'wickets': 1, 'player_id': 3, 'overs': 4.0, 'id': 1}, {'runs': 21, 'innings_id': 1, 'wickets': 3, 'player_id': 4, 'overs': 4.0, 'id': 2}, {'runs': 19, 'innings_id': 1, 'wickets': 3, 'player_id': 2, 'overs': 4.0, 'id': 3}]

and my template gives me ..

<select name="form-0-player" id="id_form-0-player">
<option value="" selected="selected">---------</option>
<option value="37">AC</option>
<option value="38">AD</option>
<option value="39">DD</option>
<option value="40">LF</option>
<option value="41">22</option>
<option value="42">dkja</option>
</select>

The selected value in this case is ----------- .. it should be my selected value, but my bowlers.value doesn't give me a name .. it gives me an id.

how can i rectify the situation?

//mouse

2012-04-05 02:42
by Yousuf Jawwad


1

Use ModelForm and instance (or queryset for formsets):

def make_bowling_form(teamid, fixtureid):
    class BowlingForm(forms.ModelForm):
        class Meta:
            model = Bowling

        player = forms.ModelChoiceField(
            queryset=PlayerShortlist.objects.filter(
                team = teamid,
                fixture = fixtureid
            )
        )
    return BowlingForm

and

BowlingFormSet = modelformset_factory(
    make_bowling_form(team_id, fixture_id),
    extra=0,
)
bowlingforms = BowlingFormSet(queryset=bowlers)
2012-04-05 04:10
by ilvar
the problem remains ... either i can have a model formset ... that contains all the objects in a PlayerShortlist .. rather than the ones i want .. if the PlayerShortlist is applied a queryset first, then the field doesn't get populated - Yousuf Jawwad 2012-04-05 19:14
Then try to limit self.fields['player'].queryset in form's \_\_init\_\_. Also, is there a field player in BowlingDetail which is ForeignKey to PlayerShortlist - ilvar 2012-04-06 05:48
Ads