{"id":301,"date":"2011-11-15T14:41:07","date_gmt":"2011-11-15T18:41:07","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=301"},"modified":"2011-11-15T15:30:39","modified_gmt":"2011-11-15T19:30:39","slug":"class-based-views-updateview-example","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2011\/11\/class-based-views-updateview-example\/","title":{"rendered":"Class Based Views &#8211; UpdateView Example"},"content":{"rendered":"<p>Yet another simple example.  I basically just took the <a href=\"http:\/\/dashdrum.com\/blog\/2011\/11\/class-based-views-deleteview-example\/\" title=\"Class Based Views \u00e2\u20ac\u201c DeleteView Example\"><code>DeleteView<\/code><\/a> example and changed a couple of things.<\/p>\n<p>Before:<\/p>\n<pre>@permission_required('calendars.change_eventtype')\r\ndef update_type(request,id):\r\n    return update_object(request,\r\n                         model=EventType,\r\n                         object_id=id,\r\n                         template_name='eventtype_form.html',\r\n                         post_save_redirect=reverse('list_type'),\r\n                       extra_context = {'calendar_menu': CalendarName.menu.all()})<\/pre>\n<p>After:<\/p>\n<pre>class TypeUpdateView(UpdateView):\r\n    template_name = 'eventtype_form.html'\r\n    model = EventType\r\n    \r\n    def get_success_url(self):\r\n        return reverse('list_type')\r\n    \r\n    ## Override dispatch to apply the permission decorator\r\n    @method_decorator(permission_required('calendars.change_eventtype'))\r\n    def dispatch(self, request, *args, **kwargs):\r\n        return super(TypeUpdateView, self).dispatch(request, *args, **kwargs)\r\n    \r\n    ## Additional context\r\n    def get_context_data(self, **kwargs):\r\n        context = super(TypeUpdateView, self).get_context_data(**kwargs)\r\n        context['calendar_menu'] = CalendarName.menu.all()\r\n        return context <\/pre>\n<p>With another example, the generic view declared a form_class instead of a model.   The model declaration is not necessary since the related model is tied to the form in its meta section.  However, in the class based generic view, both were required (or a queryset could have also been used).  I guess the related model info doesn&#8217;t make its way back to the class.  Hmmmm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yet another simple example. I basically just took the DeleteView example and changed a couple of things. Before: @permission_required(&#8216;calendars.change_eventtype&#8217;) def update_type(request,id): return update_object(request, model=EventType, object_id=id, template_name=&#8217;eventtype_form.html&#8217;, post_save_redirect=reverse(&#8216;list_type&#8217;), extra_context = {&#8216;calendar_menu&#8217;: CalendarName.menu.all()}) After: class TypeUpdateView(UpdateView): template_name = &#8216;eventtype_form.html&#8217; model = EventType def get_success_url(self): return reverse(&#8216;list_type&#8217;) ## Override dispatch to apply the permission decorator @method_decorator(permission_required(&#8216;calendars.change_eventtype&#8217;)) def dispatch(self, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2011\/11\/class-based-views-updateview-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Class Based Views &#8211; UpdateView Example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":5,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/301\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}