Django form.u数据正在忽略初始值

Django form.u数据正在忽略初始值,django,django-forms,Django,Django Forms,我有以下表格代码: class DisplaySharerForm(forms.Form): ORDER_BY_CHOICES = ( ('customer_sharer_identifier', 'customer_sharer_identifier'), ('action_count', 'action_count'), ('redirect_link', 'redirect_link'), ('enabled', 'e

我有以下表格代码:

class DisplaySharerForm(forms.Form):
    ORDER_BY_CHOICES = (
        ('customer_sharer_identifier', 'customer_sharer_identifier'),
        ('action_count', 'action_count'),
        ('redirect_link', 'redirect_link'),
        ('enabled', 'enabled'),
        ('click_total', 'click_total')
    )

    DIRECTION = (
        ('DESC','DESC'),
        ('ASC', 'ASC')
    )

    #These are the sorting options.  By default it's set to order by the customer_sharer_identifiers, descending, beginning at page 1.
    order_by = forms.ChoiceField(choices=ORDER_BY_CHOICES, required=False,initial='customer_sharer_identifier')
    direction = forms.ChoiceField(choices=DIRECTION, required=False,initial='DESC')
    action_type_id = forms.IntegerField(required=False)
    page_number = forms.IntegerField(required=False,initial=1)
以下是我尝试使用初始值创建DisplaySharerForm时得到的结果:

>>> f = DisplaySharerForm({})
>>> f.is_valid()
True
>>> f.cleaned_data
{'action_type_id': None, 'direction': u'', 'page_number': None, 'order_by': u'', 'customer_sharer_identifier': None}
为什么未将清理数据设置为我提供的初始值?我可以做些什么来修复它?

清理数据返回绑定到表单的数据的清理值。在本例中,您已将空字典绑定到表单。初始_数据用于初始表单显示和查看哪些值已更改。您可以使用自定义清理功能修复此问题:

class DisplaySharerForm(forms.Form):
...
    def clean(self):
        cleaned_data = super(DisplaySharerForm, self).clean()
        for key, value in cleaned_data.items():
            if not value and key in self.initial:
                cleaned_data[key] = self.initial[key]
        return cleaned_data
cleaned_data返回绑定到表单的数据的已清除值。在本例中,您已将空字典绑定到表单。初始_数据用于初始表单显示和查看哪些值已更改。您可以使用自定义清理功能修复此问题:

class DisplaySharerForm(forms.Form):
...
    def clean(self):
        cleaned_data = super(DisplaySharerForm, self).clean()
        for key, value in cleaned_data.items():
            if not value and key in self.initial:
                cleaned_data[key] = self.initial[key]
        return cleaned_data