更改模型表单集的标签

huangapple go评论90阅读模式
英文:

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 = [&#39;vote&#39;,]
            widgets = {
                &#39;vote&#39;: forms.Select(attrs={&quot;required&quot;: 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=&quot;yesnovote_user&quot;, null=True)
    election = models.ForeignKey(YesNoElection, on_delete=models.CASCADE, related_name=&quot;yesnovote&quot;)
    def __str__(self):
        return f&quot;{self.user} stemde {self.vote} bij {self.subtitle} (vallend onder {self.election})&quot;

and this is the html page:

&lt;form method=&quot;post&quot;&gt;
    {{ formset.management_data }}
    {% csrf_token %}
    {% for form in formset %}
    {{ form }}
    &lt;br&gt; 
    {% endfor %}
    &lt;button type=&quot;submit&quot; name = &quot;voting&quot; class=&quot;btn btn-primary save&quot;&gt;Submit&lt;/button&gt;
    Let op: Je kunt je stem niet meer wijzigen.
&lt;/form&gt;

答案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 = [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;]
    for num, form in enumerate(self.forms):
        form.fields[&quot;vote&quot;].label = label_list[num]

for more info go here https://docs.djangoproject.com/en/4.0/ref/forms/models/#modelformset-factory

huangapple
  • 本文由 发表于 2023年3月8日 16:28:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定