Python ';OneToOneField';对象没有属性';获取';在基于函数的视图中
我正在编写一个由两部分组成的表单,其中我希望将第一部分中保存的对象作为第二部分中对象的OneToOneField传递 在views.py中:Python ';OneToOneField';对象没有属性';获取';在基于函数的视图中,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我正在编写一个由两部分组成的表单,其中我希望将第一部分中保存的对象作为第二部分中对象的OneToOneField传递 在views.py中: def object_entry(request): if request.method == 'POST': title_form = TitleEntry(request.POST) object_form = ObjectEntry(request.POST) if title_form.is_
def object_entry(request):
if request.method == 'POST':
title_form = TitleEntry(request.POST)
object_form = ObjectEntry(request.POST)
if title_form.is_valid():
title = title_form.cleaned_data['title']
title_type = title_form.cleaned_data['title_type']
title_lang = title_form.cleaned_data['lang']
title_translation = title_form.cleaned_data['translation']
title_currency = title_form.cleaned_data['currency']
title_level = title_form.cleaned_data['level']
title_note = title_form.cleaned_data['note']
title_source = title_form.cleaned_data['source']
new_title = title_form.save()
return new_title
else:
return render_to_response('objectinfo/objectregister_form.html', {'object_form': object_form, 'title_form': title_form})
if object_form.is_valid():
object_form.preferred_title = new_title
snapshot = object_form.cleaned_data['snapshot']
work_type = object_form.cleaned_data['work_type']
source = object_form.cleaned_data['source']
brief_description = object_form.cleaned_data['brief_description']
description_source = object_form.cleaned_data['description_source']
comments = object_form.cleaned_data['comments']
distinguishing_features = object_form.cleaned_data['distinguishing_features']
new_object = object_form.save()
reorg.AccessionNumber.generate(new_object.pk)
return HttpResponseRedirect('/work/')
# return HttpResponseRedirect(reverse(description_edit, args=(new_object.pk,)))
else:
return render_to_response('objectinfo/objectregister_form.html', {'object_form': object_form, 'title_form': title_form})
else:
title_form = TitleEntry()
object_form = ObjectEntry()
return render(request, 'objectinfo/objectregister_form.html', {'object_form': object_form, 'title_form': title_form})
在forms.py中:
class ObjectEntry(ModelForm):
class Meta:
model = ObjectRegister
fields = ['snapshot', 'work_type', 'source', 'brief_description', 'description_source', 'comments', 'distinguishing_features', 'storage_unit', 'normal_unit']
class TitleEntry(ModelForm):
class Meta:
model = ObjectName
fields = ['title', 'title_type', 'lang', 'translation', 'currency', 'level', 'note', 'source']
提交表单时,返回错误“ObjectName”对象没有属性“get”
。回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/work/add/
Django Version: 1.10.6
Python Version: 3.6.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'reversion',
'historicdate.apps.HistoricdateConfig',
'place.apps.PlaceConfig',
'agent.apps.AgentConfig',
'storageunit.apps.StorageunitConfig',
'objectinfo.apps.ObjectinfoConfig',
'reorg.apps.ReorgConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/path/to/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
42. response = get_response(request)
File "/path/to/venv/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
138. response = self.process_response(request, response)
File "/path/to/venv/lib/python3.6/site-packages/django/middleware/clickjacking.py" in process_response
32. if response.get('X-Frame-Options') is not None:
Exception Type: AttributeError at /work/add/
Exception Value: 'ObjectName' object has no attribute 'get'
将
new\u title
传递为object\u表单的OneToOneField的正确方法是什么?首选\u title
?如果表单有效,视图将返回新实例。这是不正确的,您应该始终返回HTTP响应
if title_form.is_valid():
...
return new_title
最好在处理有效表单后重定向,以防止重复提交表单。请显示完整的回溯。is\U valid子句中的所有变量赋值似乎都没有意义。@Alasdair:谢谢,请参阅编辑的问题您正在使用的是
render
和render\u to\u response
的混合。您应该在任何地方使用render
-render\u to\u response
已过时。另外,请注意,如果对象形式为.is\u valid():,您的视图将永远无法访问,因为if title\u form.is\u valid():
的两个分支都有返回语句。您的意思是说同一个模板上不能有两个形式吗,也就是说,我应该首先有一个带有标题表单的页面,然后在该表单保存后,为目标表单显示一个单独的页面?在同一模板上可以有两个表单。当您返回响应时,最好重定向,以防止重复提交。谢谢。我仍然不知道如何使这两个表单在同一个模板上工作,所以我将它们拆分为两个单独的视图,并将第一个表单的id作为kwarg传递给第二个表单。