为Django表单选择小部件添加其他选项 [英] Add additional options to Django form select widget

查看:126
本文介绍了为Django表单选择小部件添加其他选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单用于构造一个queryeset过滤器。该表单从数据库中提取项目状态选项。但是,我想添加其他选项,例如所有现场促销...所以选择框会看起来像:




  • 所有促销*

  • 所有即时促销*

  • 草稿

  • 提交

  • 已接受

  • 已报告

  • 已选中

  • 所有已完成的促销*

  • 已关闭

  • 已取消



这里'*'是我'/ />

这是可能吗?

 code> class PromotionListFilterForm(forms.Form):
promotion_type = forms.ModelChoiceField(label =Promotion type,queryset = models.PromotionType.objects.all(),widget = forms.Select(attrs = {'class':'selector'}))
status = forms.ModelChoiceField(label =Status,queryset = models.WorkflowStatus.objects.all(),widget = forms.Select(attrs = class':'selector'}))
...
ret ailer = forms.CharField(label =Retailer,widget = forms.TextInput(attrs = {'class':'textbox'}))


解决方案

您将无法使用ModelChoiceField。您需要恢复为标准的ChoiceField,并在窗体的 __ init __ 方法中手动创建选项列表。类似于:

  class PromotionListFilterForm(forms.Form):
promotion_type = forms.ChoiceField(label =促销类型,choices =(),
widget = forms.Select(attrs = {'class':'selector'}))
....

EXTRA_CHOICES = [
('AP','所有促销'),
('LP','Live Promotions'),
('CP','完成促销'),
]

def __init __(self,* args,** kwargs):
super(PromotionListFilterForm,self).__ init __(* args,** kwargs)
choices = [(pt。 id,unicode(pt))for pt in PromotionType.objects.all()]
choices.extend(EXTRA_CHOICES)
self.fields ['promotion_type']。choices = choices

你还需要在窗体的 clean()方法来捕获这些额外的选项并适当地处理它们。


I have a form which I use to construct a queryeset filter. The form pulls in the project status options from the database. However, I wanted to add additional options, for example "All live promotions" ... so the select box would then look something like:

  • All Promotions *
  • All Live Promotions *
  • Draft
  • Submitted
  • Accepted
  • Reported
  • Checked
  • All Completed Promotions *
  • Closed
  • Canceled

Here the '*' are the ones I'd want to add and the others come from the database.

Is this possible?

class PromotionListFilterForm(forms.Form):
    promotion_type = forms.ModelChoiceField(label="Promotion Type", queryset=models.PromotionType.objects.all(), widget=forms.Select(attrs={'class':'selector'}))
    status = forms.ModelChoiceField(label="Status", queryset=models.WorkflowStatus.objects.all(), widget=forms.Select(attrs={'class':'selector'})) 
    ...
    retailer = forms.CharField(label="Retailer",widget=forms.TextInput(attrs={'class':'textbox'}))

解决方案

You won't be able to use a ModelChoiceField for that. You'll need to revert to a standard ChoiceField, and create the options list manually in the form's __init__ method. Something like:

class PromotionListFilterForm(forms.Form):
    promotion_type = forms.ChoiceField(label="Promotion Type", choices=(),
                                       widget=forms.Select(attrs={'class':'selector'}))
    ....

    EXTRA_CHOICES = [
       ('AP', 'All Promotions'),
       ('LP', 'Live Promotions'),
       ('CP', 'Completed Promotions'),
    ]

    def __init__(self, *args, **kwargs):
        super(PromotionListFilterForm, self).__init__(*args, **kwargs)
        choices = [(pt.id, unicode(pt)) for pt in PromotionType.objects.all()]
        choices.extend(EXTRA_CHOICES)
        self.fields['promotion_type'].choices = choices

You'll also need to do something clever in the form's clean() method to catch those extra options and deal with them appropriately.

这篇关于为Django表单选择小部件添加其他选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆