Rumah  >  Soal Jawab  >  teks badan

python - django中外键和多对多表单传入值,取值操作怎么做?

对于别的类型的表单数据我使用的是cleaned_data['列名']取得传入值,对于ChoiceFieldMultipleChoiceField的传入值取值应当怎么取?如果用cleaned_data['列名']方式取值分别会得到什么样的数据结构?

PHP中文网PHP中文网2741 hari yang lalu516

membalas semua(1)saya akan balas

  • 怪我咯

    怪我咯2017-04-18 09:27:35

    Kami boleh mengujinya menggunakan kod:
    model:

    class TestModel(models.Model):
    
        TYPE_CHOICE = (
            ('1', 'a'),
            ('2', 'b'),
            ('3', 'c')
        )
    
        name = models.CharField(max_length=5)
        type_choice = models.CharField(max_length=20, choices=TYPE_CHOICE)
    
        def __unicode__(self):
            return self.name
    

    Sebagai ujian, kami memasukkan tiga baris data ke dalam jadual:

    id|name|type_choice
    1|aa|a
    2|bb|b
    3|cc|c
    

    borang:

    class IndexForm(forms.ModelForm):
        TYPE_CHOICE = (
            ('1', 'a'),
            ('2', 'b'),
            ('3', 'c')
        )
    
        test_choice_type = forms.ModelChoiceField(queryset=TestModel.objects.filter(id__lte=2))
        test_multiple_choice_type = forms.ModelMultipleChoiceField(queryset=TestModel.objects.filter(id__lte=2))
    
        class Meta:
            model = TestModel
            fields = ('name', 'type_choice')
    

    Dalam borang di atas, kami mencipta ModelChoiceField dan ModelMutipleChoiceField Dalam set pertanyaan, kami menanya objek dengan id kurang daripada atau sama dengan 2.

    pandangan:

    class IndexView(FormView):
        template_name = 'index.html'
        form_class = IndexForm
    
        def form_valid(self, form):
            name = form.cleaned_data['name']
            type_choice = form.cleaned_data['type_choice']
            test_choice_type = form.cleaned_data['test_choice_type']
            test_multiple_choice_type = form.cleaned_data['test_multiple_choice_type']
    
            print type(name), name
            print type(type_choice), type_choice
            print type(test_choice_type), test_choice_type
            print type(test_multiple_choice_type), test_multiple_choice_type
    
            return super(IndexView, self).form_valid(form)
    

    Templat:

    <form action="" method="post">
        {% csrf_token %}
        {{ form|as_bootstrap }}
        <button type="submit">提交</button>
    </form>
    

    Kami mengisi borang seperti berikut.

    Hasil keluaran ialah:

    <type 'unicode'> test
    <type 'unicode'> 1
    <class 'testdjango.hei.models.TestModel'> 1
    <class 'django.db.models.query.QuerySet'> [<TestModel: 1>, <TestModel: 2>]
    

    Ia boleh dilihat daripada ini:
    ModelChoiceField mengembalikan nilai melalui cleaned_data kepada contoh model yang sepadan dengan set pertanyaan yang sepadan.
    ModelMultipleChoiceField mengembalikan senarai kejadian model yang sepadan dengan set pertanyaan yang sepadan melalui cleaned_data.
    Borang lain menggunakan nilai pulangan cleaned_data untuk menentukan jenis medannya.
    Dokumentasi dalam Django sangat jelas, anda boleh membacanya dengan teliti.

    balas
    0
  • Batalbalas