UpdateView help

379 views
Skip to first unread message

Nick

unread,
Jun 20, 2011, 5:14:26 PM6/20/11
to Django users
Help.... I am trying to use a generic view, UpdateView, to update a
form. The documentation on most of Django is great but for class-
based generic views it is sorely lacking in examples to illustrate the
changes from function views, and I am confused and frustrated.

A couple of my code snippets:


#views.py
class Update_resp(UpdateView):
model=Response
template_name="attend_edit.html"
object=get_object() # <--- this is unclear to me in the
docs... what do I do here???

#urls.py
urlpatterns = patterns('',
(r'^attend/response/edit/(?P<pk>\d+)', UpdateView.as_view()))


1) What do I do to get <pk> from the URL responder into the class-
based view, to tell the view what object I want to edit?
2) Why do I get a "name 'get_object' is not defined" error when I run
this code?

Thanks!!

Nick

Tim Shaffer

unread,
Jun 20, 2011, 8:11:43 PM6/20/11
to django...@googlegroups.com
Django automatically calls get_object() to get the object that needs to be updated by your form.

There is a default get_object() method in the SingleObjectMixin that just gets the object based on the "model" and the "pk" that's passed in:


If you need different functionality, you can overwrite that method. But otherwise, there's no need. In your case, you could write it like this:

def get_object(self):
    return Response.get(pk=self.kwargs.get('pk'))

But since that's what the default get_object() already does, there's no need for you to specify either "object" or "get_object()"

Andre Terra

unread,
Jun 20, 2011, 9:53:12 PM6/20/11
to django...@googlegroups.com
To get keywords from your url, take a look at self.kwargs.

Remember you are working with a Class now, and keeping that in mind is
fundamental to not getting frustrated with the new CBVs.
get_object() is a method that can be overridden by subclasses. Browse
the source code for the CBVs to and try to understand what are some of
the methods defined by the superclasses.

That said, the documentation is indeed lacking. Unfortunately, my
knowledge of the API is rather limited, so I was hoping other
developers could contribute to it.

Sincerely,
Andre Versa

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/IthGAEQnBMMJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Reply all
Reply to author
Forward
0 new messages