Wednesday, March 15, 2017

Re: 'OneToOneField' object has no attribute 'get' in function-based view

Here is the traceback:

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'


I ended up trying a different approach, splitting these forms into two separate views. I was able to pass the pk of the first (title) form to the second (register) through the URL and to prepopulate the latter's OneToOneField with the correct object. However, when I try to save this form, I get an ObjectDoesNotExist:

ObjectName matching query does not exist.

The traceback for this latter error follows:

Environment:      Request Method: POST  Request URL: http://127.0.0.1:8000/work/add/57/register/    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/core/handlers/base.py" in _get_response    187.                 response = self.process_exception_by_middleware(e, request)    File "/path/to/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response    185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)    File "/path/to/project/objectinfo/views.py" in object_entry    45.         object_form = ObjectEntry(request.POST, request.FILES)    File "/path/to/project/objectinfo/forms.py" in __init__    9.         self.fields['preferred_title'].initial = ObjectName.objects.get(pk=objectname_id)    File "/path/to/venv/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method    85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)    File "/path/to/venv/lib/python3.6/site-packages/django/db/models/query.py" in get    385.                 self.model._meta.object_name    Exception Type: DoesNotExist at /work/add/57/register/  Exception Value: ObjectName matching query does not exist.



Em quarta-feira, 15 de março de 2017 17:21:43 UTC-3, Vijay Khemlani escreveu:
Can you post the whole stack trace of the error?

There are some things that don't make sense in the view function (for
example returning "new_title" from the view) but they are not related
to your initial error.

On 3/15/17, Pedro Paulo Palazzo <pedro....@gmail.com> wrote:
>
>
> I am writing a two-part form where I want to pass the object saved in the
> first part as a OneToOneField for the object in the second part.
>
> In 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})
>
>
> And in 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']
>
>
> When submitting the form it returns the error 'ObjectName' object has no
> attribute 'get'. What would be the correct way to pass new_title as the
> OneToOneField of object_form.preferred_title?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/90eb2aa6-d802-4972-81ca-284b5bebe052%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0fa9eb4f-b9fb-43cc-8123-8f473c3251d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment