【问题标题】:Problem with validation. Choose the correct option. Your option is not among the valid values验证问题。选择正确的选项。您的选项不在有效值中
【发布时间】:2021-11-23 21:11:12
【问题描述】:

我是 Python 和 Django 的初学者。我正在尝试编写简单的物业管理系统。我在将现有房屋关联分配给新物业条目时遇到问题。我的文件:
models.py

class Property(models.Model):
    # Fields
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=45, blank=False) # Property name
    address = models.CharField(max_length=198, blank=False) # Property address
    postalcode = models.CharField(max_length=6, blank=False) # Property postalcode
    city = models.CharField(max_length=45, blank=False) # Property city
    kw_number = models.CharField(max_length=15, blank=True) # Numer KW
    ha_choice = models.CharField(max_length=2, choices=[(i.pk, i.name) for i in HousingAssociation.objects.all()])
    # Foreign Keys
    # TODO zmiana on_delete na inny!
    house_association = models.ForeignKey(HousingAssociation, on_delete=models.CASCADE)

    # Metadata
    class Meta:
        ordering = ['id']

    # Methods
    def add(self):
        self.save()

    def __str__(self):
        """String for representing the MyModelName object (in Admin site etc.)."""
        # return self.id, self.name, self.address, self.postalcode, self.city, self.kw_number
        return self.name

forms.py

class PropertyForm(forms.ModelForm):
    class Meta:
        model = Property
        fields = ('name', 'address', 'postalcode', 'city', 'ha_choice', 'kw_number')

views.py

def property_form_new(request):
    if request.method == "POST":
        form = PropertyForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'registration/prop/success.html')
    else:
        form = PropertyForm()
    return render(request, 'registration/prop/prop_new.html', {'property_new': form})

如果 django 返回给我这个: screenshot

这意味着'选择正确的选项。 14 不在有效值中。 14 是 db 中现有条目的 id,它是正确的。 HTML 源码是这样的:

<h2>Nowa nieruchomość</h2>
  <form method="POST" class="post-form">
    <input type="hidden" name="csrfmiddlewaretoken" value="wsrMCEU8UfLMHCBGHtKfcI1wZzGdqM8HpEAEyexx2OIDZue8Chi4DaJxsLYg7aHk">
    <p><label for="id_name">Name:</label> <input type="text" name="name" value="BCh" maxlength="45" required id="id_name"></p>
<p><label for="id_address">Address:</label> <input type="text" name="address" value="Batalionów Chłopskich 1/18" maxlength="198" required id="id_address"></p>
<p><label for="id_postalcode">Postalcode:</label> <input type="text" name="postalcode" value="15-661" maxlength="6" required id="id_postalcode"></p>
<p><label for="id_city">City:</label> <input type="text" name="city" value="Białystok" maxlength="45" required id="id_city"></p>
<ul class="errorlist"><li>Wybierz poprawną wartość. 14 nie jest żadną z dostępnych opcji.</li></ul>
<p><label for="id_ha_choice">Ha choice:</label> <select name="ha_choice" required id="id_ha_choice">
  <option value="">---------</option>

  <option value="14" selected>Spółdzielnia1</option>

  <option value="15">Spółdzielnia 2</option>

  <option value="16">flatman</option>

  <option value="17">Perspektywa</option>

  <option value="18">ŁSM</option>

  <option value="42">ŁSM</option>

  <option value="43">ŁSM 43</option>

  <option value="44">ŁSM</option>

  <option value="45">ŁSM</option>

  <option value="46">ŁSM</option>

  <option value="47">ŁSM</option>

  <option value="48">ŁSM</option>

  <option value="49">Spółdzielnia Testowa 0</option>

</select></p>
<p><label for="id_kw_number">Kw number:</label> 
<input type="text" name="kw_number" value="BI/1A/56743" maxlength="15" id="id_kw_number"></p>

    <button type="submit" class="save btn btn-default">Save</button>
  </form>


我试过

class PropertyForm(forms.ModelForm):
    ha_choice = models.ChoiceField(choices=[(i.pk, i.name) for i in HousingAssociation.objects.all()])
    class Meta:
        model = Property
        fields = ('name', 'address', 'postalcode', 'city', 'ha_choice', 'kw_number')

还有MultipleChoiceField,但效果不佳。任何人都可以帮助找到如何正确分配它吗?

【问题讨论】:

    标签: python django django-models django-views django-forms


    【解决方案1】:

    我找到了解决方案。很简单,就是换个字段

    ha_choice
    

    class PropertyForm(forms.ModelForm):
        class Meta:
            model = Property
            fields = ('name', 'address', 'postalcode', 'city', 'ha_choice', 'kw_number')
    

    house_association
    

    如此简单。

    【讨论】:

      猜你喜欢
      • 2022-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多