Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使Form和ModelForm都可以从FormMixin继承字段_Python_Django_Django Forms - Fatal编程技术网

Python 使Form和ModelForm都可以从FormMixin继承字段

Python 使Form和ModelForm都可以从FormMixin继承字段,python,django,django-forms,Python,Django,Django Forms,我有一个LocationMixin,它在表单中添加了一个位置选择器,如下所示 class LocationMixin(object): location_required = False location_label = u'location' location_text = forms.CharField(label=u'location', required=False, \ widget=for

我有一个
LocationMixin
,它在表单中添加了一个位置选择器,如下所示

class LocationMixin(object):
    location_required = False
    location_label = u'location'
    location_text = forms.CharField(label=u'location', required=False, \
                                    widget=forms.TextInput(attrs={
                                        'class': 'city_input  inputFocus proCityQueryAll proCitySelAll',
                                        'autocomplete': 'off',
                                        'readonly': 'readonly',
                                    }))

    def __init__(self, *args, **kwargs):
        super(LocationMixin, self).__init__(*args, **kwargs)
        if 'location' not in self.fields:
            raise Exception('LocationMixin need form contain field named location !')
        self.fields['location_text'].required = self.location_required
        self.fields['location_text'].label = self.location_label



class ActivateProfileForm(LocationMixin, forms.ModelForm):

    location_required = True

    class Meta:
        model = Member
        fields = ['address', 'car_type', 'car_no', 'location', 'city']
        widgets = {
            'location': HiddenInput(),
            'city': HiddenInput(),
        }
但这条线会被打破:`

self.fields['location\u text'].required=self.location\u required

Django抱怨
location\u text
self.fields
中不存在:

Traceback (most recent call last):
  File "E:\Python27\lib\site-packages\django\core\handlers\base.py", line 112, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Incubations\Project\xxx\src\xxx\decorator.py", line 54, in wrapper
    return func(request, *args, **kwargs)
  File "D:\Incubations\Project\xxx\src\xxx\views_member.py", line 166, in activate_profile
    form = ActivateProfileForm(instance=member)
  File "D:\Incubations\Project\xxx\src\location_selector\forms.py", line 25, in __init__
    self.fields['location_text'].required = self.location_required
KeyError: 'location_text'


我必须将
classlocationmixin(object):
更改为
classlocationmixin(forms.ModelForm):
以使其工作,而
classlocationmixin(forms.BaseForm)
无法工作。

问题是:

我还希望
LocationMixin
类SomeForm(LocationMixin,forms.Form)
您的LocationMixin现在不是真正的Django表单。尝试从
表单继承。表单
而不是对象

此外,根据您的目标,您可能需要颠倒主类定义的顺序。这:

class ActivateProfileForm(LocationMixin, forms.ModelForm):
不同于:

class ActivateProfileForm(forms.ModelForm, LocationMixin):

请提供完整的回溯?