{"id":553,"date":"2018-02-21T21:26:42","date_gmt":"2018-02-22T02:26:42","guid":{"rendered":"https:\/\/dashdrum.com\/blog\/?p=553"},"modified":"2018-02-21T21:30:03","modified_gmt":"2018-02-22T02:30:03","slug":"passing-a-parameter-to-a-django-form","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2018\/02\/passing-a-parameter-to-a-django-form\/","title":{"rendered":"Passing a Parameter to a Django Form"},"content":{"rendered":"<p>This is somewhat related to my previous post.  I didn&#8217;t want to include my user profile value in the form and instead add it during validation.  This caused the model validation to be skipped in part.  I had to do some gymnastics to get it to run.<\/p>\n<p>In this latest attempt, I have instead included that profile as a hidden field and compare it to the current user profile.  Much cleaner!  Now all I have to to is get that current value into the form.<\/p>\n<p>It didn&#8217;t take long on Google to find several examples.  Here is my rendition:<\/p>\n<p>In the view, I overrode the get_form method to add the profile as a paramter<\/p>\n<pre>def get_form(self, form_class=None):\r\n\r\n    if form_class is None:\r\n        form_class = self.get_form_class()\r\n    return form_class(**self.get_form_kwargs(),current_user_profile=get_profile(self.request.user))<\/pre>\n<p>Next, in the form I need to pop that value from the kwargs dictionary as assign to a object variable<\/p>\n<pre>class LinkForm(ModelForm):\r\n\r\n        def __init__(self, *args, **kwargs):\r\n\r\n            self.current_user_profile = kwargs.pop('current_user_profile')\r\n\r\n            super(LinkForm, self).__init__(*args, **kwargs)\r\n\r\n            self.fields['profile'].widget=HiddenInput()<\/pre>\n<p>I also set the profile field to hidden here.<\/p>\n<p>Now I can write a clean method for the profile field like so:<\/p>\n<pre>def clean_profile(self):\r\n\r\n        profile = self.cleaned_data.get('profile',None)\r\n\r\n        if profile != self.current_user_profile:\r\n            self.add_error(None,ValidationError('Web page altered. Try again.', code='wrong_profile'))\r\n\r\n        return profile<\/pre>\n<p>This is much easier and clearer than my previous method.  Not bad for working late on a Wednesday night.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is somewhat related to my previous post. I didn&#8217;t want to include my user profile value in the form and instead add it during validation. This caused the model validation to be skipped in part. I had to do some gymnastics to get it to run. In this latest attempt, I have instead included &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2018\/02\/passing-a-parameter-to-a-django-form\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Passing a Parameter to a Django Form&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-django"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/553","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=553"}],"version-history":[{"count":4,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/553\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}