我正在尝试在CreateView中设置保存的两个OneToOne字段的值。
以下是两个模型(每个模型都位于不同的应用程序中)。
CompanyProfile app Models.py:
class CompanyProfile(models.Model):
user = models.OneToOneField(User, related_name='user')
company_name = models.CharField(max_length=255)
CompanyData app Models.py:
class CompanyData(models.Model):
user = models.OneToOneField(User)
companyprofile = models.OneToOneField(CompanyProfile)
num_cust = models.FloatField()
我想使用CreateView设置user和companyprofile。我可以使用下面的form_valid代码设置用户。
CompanyData CreateView:
class CompanyDataCreateView(generic.CreateView):
model = models.CompanyData
fields = ['num_cust']
template_name = 'company_data/companydata_form.html'
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return super(CompanyDataCreateView, self).form_valid(form)
问题:如何同时将CompanyProfile设置为该用户的companyprofile?在保存之前有没有办法在form_valid中执行此操作?
答案 0 :(得分:2)
我假设你已经为用户创建了Traceback (most recent call last):
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/service/model.py", line 119, in wrapper
return f(dbname, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 866, in call_button
action = self._call_kw(model, method, args, {})
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 854, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 681, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 672, in call_kw_multi
result = method(recs, *args, **kwargs)
TypeError: update_info() takes at least 4 arguments (2 given)
。
因此,如果CompanyProfile
位于单独的应用中,请将其导入。
CompanyProfile
然后对CompanyProfile对象运行查询,以查找与发出请求的用户匹配的查询。
from yourproject.companyprofileapp.models import CompanyProfile
这对你有用吗?