Reputation: 5
I am currently working on an application and I have a problem with my ModelForm.
I feel like the options that I put in my ModelChoiceField is not being taken into account.
This is my forms.py:
class PlayerModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return '{} from {} at {} '.format(obj.name, obj.team, obj.price)
class Form(forms.ModelForm):
def __init__(self, contest_id, *args, **kwargs):
super(Form, self).__init__(*args, **kwargs)
contest = ContestLOL.objects.filter(id=contest_id)[0]
teams = contest.teams.all()
players = Player.objects.filter(team__in=teams).order_by('-price')
self.fields['top'].queryset = players.filter(role='Top')
self.fields['jungle'].queryset = players.filter(role='Jungle')
self.fields['mid'].queryset = players.filter(role='Mid')
self.fields['adc'].queryset = players.filter(role='ADC')
self.fields['support'].queryset = players.filter(role='Support')
self.fields['top'].initial = players.filter(role='Top')[0]
self.fields['jungle'].initial = players.filter(role='Jungle')[0]
self.fields['mid'].initial = players.filter(role='Mid')[0]
self.fields['adc'].initial = players.filter(role='ADC')[0]
self.fields['support'].initial = players.filter(role='Support')[0]
class Meta:
model = Model
fields = {'top': PlayerModelChoiceField(
queryset=Player.objects.filter(role='Top'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name="top"),
'jungle': PlayerModelChoiceField(
queryset=Player.objects.filter(role='Jungle'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name="jungle"),
'mid': PlayerModelChoiceField(
queryset=Player.objects.filter(role='Mid'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name="mid"),
'adc': PlayerModelChoiceField(
queryset=Player.objects.filter(role='ADC'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name="adc"),
'support': PlayerModelChoiceField(
queryset=Player.objects.filter(role='Support'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name="supp")
}
fields = {'top', 'jungle', 'mid', 'adc', 'support'}
This is is my view.py:
def contest(request, slug=None):
contest = get_object_or_404(ContestLOL, slug=slug)
matches= Match.objects.filter(contest=contest.id)
form = Form(contest.id, request.POST or None)
if form.is_valid():
bet = form.save(commit=False)
bet.user = request.user
bet.contest = contest
bet.save()
messages.success(request, 'Bet was saved')
form = Form(contest.id)
output = { "contest": contest,
"form": form,
"matches": matches
}
return render(request, "questions/contest.html", output)
And this is contest.html:
<form method="post">
{% csrf_token %}
Top :
<br>
{{form.top}}
<hr>
Jungle :
<br>
{{form.jungle}}
<hr>
Mid :
<br>
{{form.mid}}
<hr>
ADC :
<br>
{{form.adc}}
<hr>
Support :
<br>
{{form.support}}
<hr>
<input class="btn btn-success" type="submit" value='Submit'>
</form>
And a screenshot of what I get is:
[enter image description here][1]
https://i.sstatic.net/0dOaC.png
As you can see, there is no RadioSelectWidget and there is still the empty label. So that makes me think, that the options of the ModelChoiceField is not working. Did anyone had the same case before.
Upvotes: 0
Views: 106
Reputation: 476719
Well you define fields
twice in your Meta
, hence only the last one will persist. But even if you would use the dictionary you use, that is not the way to define fields. You define the fields that the form class level, so:
class Form(forms.ModelForm):
top = PlayerModelChoiceField(
queryset=Player.objects.filter(role='Top'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='top'
)
jungle = PlayerModelChoiceField(
queryset=Player.objects.filter(role='Jungle'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='jungle'
)
mid = PlayerModelChoiceField(
queryset=Player.objects.filter(role='Mid'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='mid'
)
adc = PlayerModelChoiceField(
queryset=Player.objects.filter(role='ADC'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='adc'
)
support = PlayerModelChoiceField(
queryset=Player.objects.filter(role='Support'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='supp'
)
def __init__(self, contest_id, *args, **kwargs):
super(Form, self).__init__(*args, **kwargs)
self.fields['top'].initial = players.filter(role='Top')[0]
self.fields['jungle'].initial = players.filter(role='Jungle')[0]
self.fields['mid'].initial = players.filter(role='Mid')[0]
self.fields['adc'].initial = players.filter(role='ADC')[0]
self.fields['support'].initial = players.filter(role='Support')[0]
class Meta:
model = Model
fields = ('top', 'jungle', 'mid', 'adc', 'support')
Upvotes: 1