2

我有一个类似于以下的表格(为简洁起见):

PRICING_PATTERN = r'(?:^\$?(?P<flat_price>\d+|\d?\.\d\d)$)|(?:^(?P<percent_off>\d+)\s*\%\s*off$)'
class ItemForm(forms.Form):
    pricing = forms.RegexField(
        label='Pricing',
        regex=PRICING_PATTERN
    )

    pricing_type = forms.CharField(
        label='Deal type',
        widget=forms.RadioSelect(
            choices=(
                ('flat_price','Flat price'),
                ('percent_off','Percentage off'),
            ),
           attrs={'style': 'display: none;'})
        ),
    )

    pricing_flat_price = forms.DecimalField(
        label='Flat price',
        max_digits=5,
        decimal_places=2,
        widget=forms.TextInput(attrs={'style': 'display: none;'})
    )

    pricing_percent_off = forms.IntegerField(
        label='Percent off',
        required=False,
        min_value=0,
        max_value=100,
        widget=forms.TextInput(attrs={'style': 'display: none;'})
    )

最初,出于优雅降级的目的,只有定价是可见的。如果用户启用了 javascript,我会隐藏定价并使定价类型可见。现在,在pricing_type单选选项中,我将pricing_flat_costpricing_percent_off 设为可见。这使得 UI 更加精确和用户友好。

我的问题:我应该如何编写逻辑来确定从何处获取值——RegexField 或pricing_flat_pricepricing_percent_off字段?我是否应该在ItemForm中创建一个函数来计算它并返回正确的值?

或者也许有人可以建议一种更清洁的方法?

4

1 回答 1

1

为类型选择定义一个自定义小部件,并将 JavaScript 代码作为单独的 .js 文件包含在内。

于 2010-10-11T20:11:44.803 回答