{"id":311,"date":"2011-12-12T14:14:34","date_gmt":"2011-12-12T18:14:34","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=311"},"modified":"2012-01-29T10:46:51","modified_gmt":"2012-01-29T14:46:51","slug":"using-form-savecommitfalse-in-a-class-based-view","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2011\/12\/using-form-savecommitfalse-in-a-class-based-view\/","title":{"rendered":"Using form.save(commit=False) in a Class Based View"},"content":{"rendered":"<p><strong>UPDATE: I found a better way to do this. \u00c2\u00a0<a title=\"A Better Way \u00e2\u20ac\u201c Don\u00e2\u20ac\u2122t!\" href=\"http:\/\/dashdrum.com\/blog\/2012\/01\/a-better-way-dont\/\">See this post<\/a>.<\/strong><\/p>\n<p>In this example, I once again hadn&#8217;t used a generic view for this create function, since I needed to add a value that is not in the form to the new object before the save.<\/p>\n<pre>    if form.is_valid():\r\n        new_rec = form.save(commit=False)\r\n        new_rec.institution = institution\r\n        new_rec.save()\r\n        return HttpResponseRedirect(reverse('cat_list'))<\/pre>\n<p>The value for <code>institution<\/code> comes from the user&#8217;s session area. The above is the accepted pattern to add the data to the object.<\/p>\n<h2>So how can we do this in a class based view?<\/h2>\n<p>An override of <code>form_valid()<\/code> in the class will handle this case.<\/p>\n<pre>    def form_valid(self, form):\r\n        self.object = form.save(commit=False)\r\n        self.object.institution = self.kwargs['institution']\r\n        self.object.save()\r\n        return HttpResponseRedirect(self.get_success_url())<\/pre>\n<p>(Putting the institution value in kwargs is the subject for another post. One could also use an object variable.)<\/p>\n<p><strong>UPDATE: I found a better way to do this. \u00c2\u00a0<a title=\"A Better Way \u00e2\u20ac\u201c Don\u00e2\u20ac\u2122t!\" href=\"http:\/\/dashdrum.com\/blog\/2012\/01\/a-better-way-dont\/\">See this post<\/a>.<\/strong><\/p>\n<p>The reader who just wants to know how to do this can stop here. However&#8230;.<\/p>\n<h2>Is this the best way to do this?<\/h2>\n<p>I&#8217;m not very skilled in the art of overriding methods, so I am a little concerned about the forward compatibility of this solution.<br \/>\nLet&#8217;s first see the upstream definitions of this method.<br \/>\nThe method is defined in ModelFormMixin as follows:<\/p>\n<pre>    def form_valid(self, form):\r\n        self.object = form.save()\r\n        return super(ModelFormMixin, self).form_valid(form)<\/pre>\n<p>And above that we find a simpler method in FormMixin:<\/p>\n<pre>    def form_valid(self, form):\r\n        return HttpResponseRedirect(self.get_success_url())<\/pre>\n<p>I like how the ModelFormMixin method is able to refer back to the original using the super function, but I don&#8217;t know how I can jump over the immediate ancestor to call back the original method. Is this possible? Does it matter?<\/p>\n<p>I am slightly concerned that changes to the method in FormMixin that may come along in future versions may be overlooked with this solution. Sure, I have to check these things during an upgrade anyway, but I would like to perform this as &#8220;correctly&#8221; as possible.<\/p>\n<p>Please leave your suggestions, thoughts and comments below.<\/p>\n<p><strong>UPDATE: I found a better way to do this. \u00c2\u00a0<a title=\"A Better Way \u00e2\u20ac\u201c Don\u00e2\u20ac\u2122t!\" href=\"http:\/\/dashdrum.com\/blog\/2012\/01\/a-better-way-dont\/\">See this post<\/a>.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: I found a better way to do this. \u00c2\u00a0See this post. In this example, I once again hadn&#8217;t used a generic view for this create function, since I needed to add a value that is not in the form to the new object before the save. if form.is_valid(): new_rec = form.save(commit=False) new_rec.institution = institution &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2011\/12\/using-form-savecommitfalse-in-a-class-based-view\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using form.save(commit=False) in a Class Based View&#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-311","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/311","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=311"}],"version-history":[{"count":5,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}