{"id":329,"date":"2012-01-29T10:44:47","date_gmt":"2012-01-29T14:44:47","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=329"},"modified":"2012-01-29T11:12:39","modified_gmt":"2012-01-29T15:12:39","slug":"a-better-way-dont","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2012\/01\/a-better-way-dont\/","title":{"rendered":"A Better Way &#8211; Don&#8217;t!"},"content":{"rendered":"<p>In an <a title=\"Using form.save(commit=False) in a Class Based View\" href=\"http:\/\/dashdrum.com\/blog\/2011\/12\/using-form-savecommitfalse-in-a-class-based-view\/\">earlier post<\/a>, I talked about using <code>form.save(commit=False)<\/code> in a class based view, and I showed a way to do it. \u00c2\u00a0However, I wasn&#8217;t looking far enough back to see the real question:<\/p>\n<p><em>How can I add information not found on the form to the object before saving?<\/em><\/p>\n<p>Thanks to a reply way down in this <a title=\"Class-Based Generic Views (CreateView) - field exclusions and defaults\" href=\"http:\/\/groups.google.com\/group\/django-users\/browse_thread\/thread\/f8a9832dcb5e9e87\">Google Groups post<\/a>, I found what I needed, and I kicked my self for not seeing this earlier. Since I was using the default form logic to save the object, I could send the additional value to the form.<\/p>\n<pre>    ## Include the instance object before saving\r\n    def form_valid(self, form):\r\n        form.instance.institution = self.institution\r\n        return super(EventCreateView,self).form_valid(form)<\/pre>\n<p>I like this solution better than what I provided in the <a title=\"Using form.save(commit=False) in a Class Based View\" href=\"http:\/\/dashdrum.com\/blog\/2011\/12\/using-form-savecommitfalse-in-a-class-based-view\/\">earlier post<\/a> because it is a better object oriented approach.<\/p>\n<p>Of course, I have used the older example all over my applications, so I&#8217;ve got some refactoring to do.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an earlier post, I talked about using form.save(commit=False) in a class based view, and I showed a way to do it. \u00c2\u00a0However, I wasn&#8217;t looking far enough back to see the real question: How can I add information not found on the form to the object before saving? Thanks to a reply way down &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2012\/01\/a-better-way-dont\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Better Way &#8211; Don&#8217;t!&#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-329","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/329","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=329"}],"version-history":[{"count":5,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions\/337"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}