{"id":547,"date":"2018-01-25T14:01:14","date_gmt":"2018-01-25T19:01:14","guid":{"rendered":"https:\/\/dashdrum.com\/blog\/?p=547"},"modified":"2018-01-25T14:01:14","modified_gmt":"2018-01-25T19:01:14","slug":"execute-validate_unique-when-form-doesnt-include-all-fields","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2018\/01\/execute-validate_unique-when-form-doesnt-include-all-fields\/","title":{"rendered":"Execute validate_unique when form doesn&#8217;t include all fields"},"content":{"rendered":"<p>Asked this question on Stack Overflow today:<\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/48449821\/execute-validate-unique-when-form-doesnt-include-all-fields\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/48449821\/execute-validate-unique-when-form-doesnt-include-all-fields<\/a><\/p>\n<p>I recently had a situation where the validate_unique method from my model wasn&#8217;t running. This is because one of the fields involved in the unique test wasn&#8217;t included in the form. I tried many things in the form and the view before landing on this solution: I first injected the field into the object of the UpdateView, then ran the test in the Form in _post_clean.<\/p>\n<pre>models.py \r\n\r\nclass Link(ModelBase):\r\n\tid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)\r\n\ttitle = models.CharField(max_length=200,blank=True,null=False)\r\n\turl = models.URLField(max_length=400,blank=False,null=False)\r\n\tprofile = models.ForeignKey('Profile',null=False,blank=False,on_delete=models.CASCADE)\r\n\r\n\tclass Meta:\r\n\t\tunique_together = ('url','profile')\r\n\r\n\tclass Admin:\r\n\t\tpass<\/pre>\n<pre>forms.py \r\n\r\nclass LinkForm(ModelForm):\r\n\r\n\tdef _post_clean(self):\r\n\t\t''' Be sure that the instance validate_unique method is run including the profile field '''\r\n\t\tsuper(LinkForm,self)._post_clean()\r\n\t\ttry:\r\n\t\t\tself.instance.validate_unique(exclude=None)\r\n\t\texcept ValidationError as e:\r\n\t\t\tself._update_errors(e)\r\n\r\n\tclass Meta:\r\n\t\tmodel = Link\r\n\t\tfields = ['title','url']<\/pre>\n<pre>views.py \r\n\r\nclass LinkUpdateView(UpdateView):\r\n\tmodel = Link\r\n\tform_class = LinkForm\r\n\r\n\tdef get_form_kwargs(self):\r\n\t\t''' Add profile to self.object before kwargs are populated '''\r\n\r\n\t\tif hasattr(self, 'object') and self.object and self.profile:\r\n\t\t\t\tself.object.profile = profile\r\n\r\n\t\tkwargs = super(LinkUpdateView, self).get_form_kwargs()\r\n\r\n\t\treturn kwargs<\/pre>\n<p>Is there a better way to do this that doesn&#8217;t involve overriding an internal function?<\/p>\n<p>Got any help for me? Please post an answer here or on SO. I&#8217;ll share the solution here once I get one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Asked this question on Stack Overflow today: https:\/\/stackoverflow.com\/questions\/48449821\/execute-validate-unique-when-form-doesnt-include-all-fields I recently had a situation where the validate_unique method from my model wasn&#8217;t running. This is because one of the fields involved in the unique test wasn&#8217;t included in the form. I tried many things in the form and the view before landing on this solution: I &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2018\/01\/execute-validate_unique-when-form-doesnt-include-all-fields\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Execute validate_unique when form doesn&#8217;t include all fields&#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":[],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/547","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=547"}],"version-history":[{"count":4,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":551,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions\/551"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}