Django 模型中的每个字段都显示 "Select a valid choice. That choice is not one of the available choices."

Every field in Django modelform shows "Select a valid choice. That choice is not one of the available choices."

我已经阅读了许多其他线程抱怨此错误消息,但我仍然无法弄清楚。我尝试删除给出错误的字段,并且错误消息只是在我下次尝试提交时移动到另一个字段。分别是CharField、Foreign Key等类型。

forms.py

class TemporaryresponseForm(forms.ModelForm):    
    gender_custom = forms.CharField(
        required=False,
        label="",        
    )

    ethnicity = forms.ModelChoiceField(
        queryset=Ethnicity.objects.all(),
        widget=forms.RadioSelect(),
        empty_label=None,
        required=True,
        label="Which of the following best describes your ethnicity?"
    )
...
class Meta:
        model = Temporaryresponse
        fields = [...'gender_custom', 'ethnicity',...]

views.py

def tr(request):
    if request.method == "POST":
        form = TemporaryresponseForm(request.POST)
        if form.is_valid():
            tempresponse = form.save(commit=False)
            tempresponse.ip = "123456"
            tempresponse.save()
            return redirect('politicalpollingapp/index.html')
    else:
        form = TemporaryresponseForm()
    return render(request, 'politicalexperimentpollapp/tr.html', {'form': form})


def nr(request, pk):
    return render(request, 'politicalexperimentpollapp/nr.html', {'tempresponse': tempresponse})

tr.html template

{% extends 'politicalexperimentpollapp/base.html' %}
{% block extrahead %}
{% load crispy_forms_tags %}
{{ form.media }}
{% endblock extrahead%} 
...
<form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <div><br></div>

        <div class="text-center"><button type="submit" class="save btn btn-primary">CONTINUE</button></div>
    </form>
..

models.py

class Ethnicity(models.Model):
    ethnicity = models.CharField(max_length=200)

    def __str__(self):
        return '%s' % (self.ethnicity)
...
class Temporaryresponse(models.Model):
    birth_year = models.PositiveIntegerField()
    voting_registration = models.ForeignKey(Voting_registration, models.SET_NULL, null=True)
    party_identification = models.ForeignKey(Party_identification, models.SET_NULL, null=True)
    gender = models.ForeignKey(Gender, models.SET_NULL, null=True)
    gender_custom = models.CharField(max_length=200, blank=True)
    ethnicity = models.ForeignKey(Ethnicity, models.SET_NULL, null=True)
    race = models.ManyToManyField(Race)
    zip_code = models.IntegerField()
    ip = models.CharField(max_length=200, blank=True)
    policy_areas_of_importance = models.ManyToManyField(Policy_category, blank=True)
    likelihood_of_voting = models.PositiveIntegerField(models.SET_NULL, null=True, blank=True)

奇怪的是,我的 Chrome 控制台中没有显示错误 - 这只是因为我在实际页面上显示错误。我不确定这是否正常。在此先感谢您的帮助,此时我正在努力。

我发现我在 "race" 表单域中使用了错误的语言。我用过 ModelChoiceField 但它应该是 ModelMultipleChoiceField 如下:

race = forms.ModelMultipleChoiceField(queryset=Race.objects.all(), widget=forms.CheckboxSelectMultiple, label="5. Which of the following best describes your race? Please select all that apply.")