' OneToOneField'对象没有属性' get'在基于功能的视图中

时间:2017-03-15 18:34:09

标签: 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_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' object has no attribute '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_title作为object_form.preferred_title的OneToOneField传递的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

如果表单有效,您的视图将返回新实例。这是不正确的,您应该始终返回HTTP响应。

    if title_form.is_valid():
        ...
        return new_title

在处理完有效表单后重定向是个好主意,以防止重复提交表单。