Django populate a form.ChoiceField field from a queryset and relate the choice back to the model object

后端 未结 2 1281
小鲜肉
小鲜肉 2021-02-05 20:48

I have a simple form:

class SubmissionQuickReplyForm(forms.Form):
    comment_text = forms.CharField(label=\'\', required=False, widget=forms.Textarea(attrs={\'r         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 20:52

    For ChoiceField you can use

        choice = forms.ChoiceField(choices=[
        (choice.pk, choice) for choice in MyChoices.objects.all()])
    

提交回复
热议问题