Django 表单缓存填充表单

1 投票
3 回答
1689 浏览
提问于 2025-04-17 13:00

我在Django中填充一个表单的选项,

问题是,当我在浏览器中查看时,年份列表是根据数据库显示的没错,但当我在数据库中更改了一些日期后,年份的选择列表却没有更新。

class PlanForm(forms.Form):

    def get_prices():
        return forms.ChoiceField(
            choices=map(
                lambda x: (
                    x.pk, 
                    (
                        unicode(x.length) 
                        + _(' Day Listing / ') 
                        + unicode(x.pics) 
                        + _(' Photos: ') 
                        + _('$ ') 
                        + unicode(x.price)
                    ),
                ),
                Pricing.objects.filter(
                    enable=True,
                    site=settings.SITE_ID,
                ),
            ),
            label=_('Plans'),
            widget=forms.RadioSelect
        )

    def get_options():
        return forms.MultipleChoiceField(
            choices=map(
                lambda x: (
                    x.pk, 
                    (
                        unicode(x) 
                        + _(' : $') 
                        + unicode(x.price)
                    ),
                ),
                PricingOptions.objects.filter(
                    enable=True,
                    site=settings.SITE_ID,
                )
            ),
            label=_('Ads Options'),
            widget=forms.CheckboxSelectMultiple,
            required=False
        )

    def __init__(self, *args, **kwargs):
        print 'passou no init'
        super(PlanForm, self).__init__(*args, **kwargs)
        self.fields['pricing'] = self.get_prices()
        self.fields['pricing_options'] = self.get_options()


    pricing = get_prices()
    pricing_options = get_options()

我尝试了这段代码,但出现了错误:

get_prices() 不接受参数(给了1个)

我该怎么解决这个问题呢?

3 个回答

0

试试看:

class PlanForm(forms.Form):

    def __init__(self, *args, **kwargs):
        print 'passou no init'
        super(PlanForm, self).__init__(*args, **kwargs)
        self.fields['pricing'] = forms.ChoiceField(choices=map(lambda x: (x.pk, unicode(x.length) + _(' Day Listing / ') + unicode(x.pics) +  _(' Photos: ') + _('$ ') + unicode(x.price)  ), Pricing.objects.filter(enable=True,site=settings.SITE_ID)),label=_('Plans'), widget=forms.RadioSelect)
        self.fields['pricing_options'] =  forms.MultipleChoiceField(choices=map(lambda x: (x.pk, unicode(x) + _(' : $') + unicode(x.price)), PricingOptions.objects.filter(enable=True,site=settings.SITE_ID)),label=_('Ads Options'), widget=forms.CheckboxSelectMultiple, required=False)
0

类方法(比如 get_prices)至少需要一个参数 self。这个 self 参数会被填充为调用这个方法的类实例,也就是说,它代表了当前这个对象。

0

给你的方法加上 @staticmethod 装饰器。Python 默认会把在类里面定义的函数当作方法来处理,并且会自动把一个叫 'self' 的参数传给它。使用 staticmethod 可以阻止这种默认行为。

class PlanForm(forms.Form):

    @staticmethod
    def get_prices():
        return forms.ChoiceField(choices=map(lambda x: (x.pk, unicode(x.length) + _(' Day Listing / ') + unicode(x.pics) +  _(' Photos: ') + _('$ ') + unicode(x.price)  ), Pricing.objects.filter(enable=True,site=settings.SITE_ID)),label=_('Plans'), widget=forms.RadioSelect)

    @staticmethod
    def get_options():
        return forms.MultipleChoiceField(choices=map(lambda x: (x.pk, unicode(x) + _(' : $') + unicode(x.price)), PricingOptions.objects.filter(enable=True,site=settings.SITE_ID)),label=_('Ads Options'), widget=forms.CheckboxSelectMultiple, required=False)

    def __init__(self, *args, **kwargs):
        print 'passou no init'
        super(PlanForm, self).__init__(*args, **kwargs)
        self.fields['pricing'] = self.get_prices()
        self.fields['pricing_options'] = self.get_options()


    pricing = get_prices()
    pricing_options = get_options()

撰写回答