django-multiselectfield和表单的问题

时间:2019-07-24 18:43:39

标签: python django django-models django-forms

我想在模型django中进行多重选择及其结果(我使用了library),现在我尝试确保仅从模型字段中显示选择的项。表格,但我面临一个小问题:

AttributeError: 'MSFList' object has no attribute 'get_choices_selected'

1)我知道在该库中有一个函数get_choices_selected返回选定的项目

2)我试图仅使用模型的输出,但是它返回字符串数据类型:((

我的模型。py

CHOSE_SIZE = (('XS', 'XS'),
          ('S', 'S'),
          ('M', 'M'),
          ('L', 'L'),
          ('XL', 'XL'),
          ('XXL', 'XXL'),
          ('XXXL', 'XXXL'))

          .......

           available_size = MultiSelectField(choices=CHOSE_SIZE, verbose_name='Available Sizes', default=None)

我的表格。py

class item_add_form(forms.Form):
    quantity = forms.IntegerField(max_value=21, min_value=0)
    size = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOSE_SIZE)

    def __init__(self, *args, **kwargs):
        choices = kwargs.pop('choices', None)
        super(item_add_form, self).__init__(*args, **kwargs)
        self.fields['size'] =  forms.ChoiceField(widget=forms.RadioSelect, choices=choices)

我的views.py

class MainPage(views.View):
    products = Product.objects.get(ordered__gt=0)
    form = item_add_form(choices =products.available_size.get_choices_selected())
    contex = {'products' : products, 'form_input': form }
    def get(self, request):
        return render(request, 'MainPage/Mainpage.html', context=self.contex)

我将很高兴为您提供帮助,并向您提出建议:

1)是否可以以某种方式解决此问题?

2)或如何做得更好?

0 个答案:

没有答案