英文:
Changing modelformset labels
问题
I have created a modelformset for voting on multiple subtitles. It looks like this:
I want to change the "Vote:" label to the names of the different subtitles instead (so if the subtitles are A, B and C, A should be before the first field, B for the second field, etc.). How can I change this?
The modelformset is created in the views.py like this:
VoteFormSet = modelformset_factory(YesNoVote, form=YesNoVotingForm, extra=3)
YesNoVotingForm looks like this:
class YesNoVotingForm(forms.ModelForm):
class Meta:
model = YesNoVote
fields = ['vote',]
widgets = {
'vote': forms.Select(attrs={"required": True}),
}
and this is the yesnovote model:
class YesNoVote(models.Model):
subtitle = models.CharField(max_length=128)
vote = models.CharField(choices=choices, max_length=10)
user = models.ForeignKey(User, on_delete=models.CASCADE, db_constraint=False, related_name="yesnovote_user", null=True)
election = models.ForeignKey(YesNoElection, on_delete=models.CASCADE, related_name="yesnovote")
def __str__(self):
return f"{self.user} stemde {self.vote} bij {self.subtitle} (vallend onder {self.election})"
and this is the html page:
<form method="post">
{{ formset.management_data }}
{% csrf_token %}
{% for form in formset %}
{{ form }}
<br>
{% endfor %}
<button type="submit" name="voting" class="btn btn-primary save">Submit</button>
Let op: Je kunt je stem niet meer wijzigen.
</form>
英文:
I have created a modelformset for voting on multiple subtitles. It looks like this:
I want to change the "Vote:" label to the names of the different subtitles instead (so if the subtitles are A, B and C, A should be before the first field, B for the second field etc.). How can I change this?
The modelformset is created in the views.py like this:
VoteFormSet = modelformset_factory(YesNoVote, form=YesNoVotingForm, extra=3)
YesNoVotingForm looks like this:
class YesNoVotingForm(forms.ModelForm):
class Meta:
model = YesNoVote
fields = ['vote',]
widgets = {
'vote': forms.Select(attrs={"required": True}),
}
and this is the yesnovote model:
class YesNoVote(models.Model):
subtitle = models.CharField(max_length=128)
vote = models.CharField(choices=choices, max_length=10)
user = models.ForeignKey(User, on_delete=models.CASCADE, db_constraint=False, related_name="yesnovote_user", null=True)
election = models.ForeignKey(YesNoElection, on_delete=models.CASCADE, related_name="yesnovote")
def __str__(self):
return f"{self.user} stemde {self.vote} bij {self.subtitle} (vallend onder {self.election})"
and this is the html page:
<form method="post">
{{ formset.management_data }}
{% csrf_token %}
{% for form in formset %}
{{ form }}
<br>
{% endfor %}
<button type="submit" name = "voting" class="btn btn-primary save">Submit</button>
Let op: Je kunt je stem niet meer wijzigen.
</form>
答案1
得分: 0
如果您有精确数量的表单集,请尝试以下操作:
在views.py中添加modelformset_factory的表单集:
VoteFormSet = modelformset_factory(YesNoVote, form=YesNoVotingForm, extra=3, formset=YesNoVoteModelFormSet)
在forms.py中创建:
class YesNoVoteModelFormSet(forms.BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
label_list = ["A", "B", "C"]
for num, form in enumerate(self.forms):
form.fields["vote"].label = label_list[num]
获取更多信息,请访问此处:https://docs.djangoproject.com/en/4.0/ref/forms/models/#modelformset-factory
英文:
So if you have exact number of formsets try:
in views.py add formset for modelformset_factory:
VoteFormSet = modelformset_factory(YesNoVote, form=YesNoVotingForm, extra=3, formset=YesNoVoteModelFormSet)
in forms.py create:
class YesNoVoteModelFormSet(forms.BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
label_list = ["A", "B", "C"]
for num, form in enumerate(self.forms):
form.fields["vote"].label = label_list[num]
for more info go here https://docs.djangoproject.com/en/4.0/ref/forms/models/#modelformset-factory
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论