【发布时间】:2018-04-25 16:22:08
【问题描述】:
我收到以下类型错误,这是由于查看此实例所致。您可以在下面看到的模型中,Employee 是与 User 的一对一关系,Company 是 Employee 的外键。我如何通过获取 company 实例来解决这个问题?或者是什么问题。
这就是问题所在:
companysetting_form = CompanySettingEdit(instance = request.user.employee.company)
下面是类型错误
TypeError at /employee/companysettings/
__init__() got an unexpected keyword argument 'instance'
Request Method: GET
Request URL: http://127.0.0.1:8000/employee/companysettings/
Django Version: 1.11.7
Exception Type: TypeError
Exception Value:
__init__() got an unexpected keyword argument 'instance'
Exception Location: /Users/megasap/Documents/project/railercom/railercomapp/views.py in employee_companysettings, line 83
Python Executable: /Users/megasap/Documents/project/myvirtualenv/railercom/bin/python
Python Version: 3.6.3
Python Path:
['/Users/megasap/Documents/project/railercom',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/Users/megasap/Documents/project/myvirtualenv/railercom/lib/python3.6/site-packages']
下面是我的代码:
https://gitlab.com/firdausmah/railercom/blob/master/railercomapp/views.py
@login_required(login_url='/employee/sign-in/')
def employee_companysettings(request):
companysetting_form = CompanySettingEdit(instance = request.user.employee.company) <---- this is the problem
if request.method == "POST":
companysetting_form = CompanySettingEdit(request.POST, instance = request.user.employee.company)
if companysetting_form.is_valid():
companysetting_form.save()
return render(request, 'employee/account.html', {
"companysetting_form":companysetting_form
})
https://gitlab.com/firdausmah/railercom/blob/master/railercomapp/forms.py
class CompanySettingEdit(forms.Form):
name = forms.CharField(max_length=50, required=False)
tel = forms.CharField(max_length=50, required=False)
address_1 = forms.CharField(max_length=50, required=False)
address_2 = forms.CharField(max_length=50, required=False)
address_zip = forms.CharField(max_length=50, required=False)
address_city = forms.CharField(max_length=50, required=False)
address_state = forms.CharField(max_length=50, required=False)
address_country = forms.CharField(max_length=50, required=False)
class Meta:
model = Company
fields = ("name", "tel", "address_1", "address_2", "address_zip",
"address_city","address_state","address_country")
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form companysetting_form %}
<button type="submit" class="btn btn-pink">Update</button>
</form>
https://gitlab.com/firdausmah/railercom/blob/master/railercomapp/models.py
class Company(models.Model):
name = models.CharField(max_length=50)
tel = models.CharField(max_length=15, blank=True)
address_1 = models.CharField(max_length=50, blank=True)
address_2 = models.CharField(max_length=50, blank=True)
address_zip = models.CharField(max_length=20, blank=True)
address_city = models.CharField(max_length=20, blank=True)
address_state = models.CharField(max_length=20, blank=True)
address_country = models.CharField(max_length=20, blank=True)
logo = models.ImageField(upload_to='company_logo/', blank=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
【问题讨论】:
-
forms.Form的构造函数似乎不接受instance关键字参数。 -
感谢您的评论,您能给一些指导吗?此表单有效,但我需要将表单数据保存到模型中。我需要在表单构造函数中有 init 并做一些事情吗?谢谢。 gist.github.com/anonymous/db51370aaf0ce759ab51cb24fc390633
标签: python django view foreign-keys instance