从视图到表单传递request.user.id不起作用:(

时间:2016-05-07 22:19:12

标签: django django-admin

views.py

def RealStateView(request):
    form = RealStateForm(request.user)

forms.py

class RealStateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    request = kwargs.pop('request')
    super(RealStateForm, self).__init__(*args, **kwargs)
    self.fields['user_id'].initial = request.user.id


class Meta:
    model = RealState
    exclude = ["owner"]
    fields = '__all__'

def clean_acquisition_value(self):
    message = self.cleaned_data['acquisition_value']

    if message < 10:
        raise ValidationError('No puede ser cero')
    return message

错误说:

Exception Type: KeyError
Exception Value: 'request'

我尝试过其他没有成功结果的方法..有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您没有将名为“request”的关键字参数传递给构造函数。相反,您传递的值为request.user的位置参数。

form = RealStateForm(request=request)