{"id":1497,"date":"2017-07-23T19:21:07","date_gmt":"2017-07-24T02:21:07","guid":{"rendered":"https:\/\/www.laurenwood.org\/anyway\/?p=1497"},"modified":"2017-07-23T19:21:07","modified_gmt":"2017-07-24T02:21:07","slug":"tags-and-case","status":"publish","type":"post","link":"https:\/\/www.laurenwood.org\/anyway\/2017\/07\/tags-and-case\/","title":{"rendered":"Tags and&nbsp;case"},"content":{"rendered":"<p>For a while there <a href=\"https:\/\/www.xml.com\"><span class=\"caps\">XML<\/span>.com<\/a> did\u00adn\u2019t handle tags on <a href=\"https:\/\/www.xml.com\/news\/submit-news-item\/\">sub\u00admit\u00adted news items<\/a> very well. If a tag was included that was in a dif\u00adfer\u00adent case to an exist\u00ading tag, the pre\u00adview and pub\u00adlish would res\u00adult in a 500 serv\u00ader error. For\u00adtu\u00adnately this was some\u00adthing that was\u00adn\u2019t vis\u00adible to the out\u00adside world, but annoy\u00ading nonetheless.<\/p>\n<p><a href=\"https:\/\/wagtail.io\/\">Wag\u00adtail<\/a> allows case-insens\u00adit\u00adive tags, and I had already turned that on (it would be con\u00adfus\u00ading to have searches for the tags \u201c<span class=\"caps\">XSLT<\/span>\u201d and \u201cxslt\u201d return dif\u00adfer\u00adent res\u00adults, for example). Art\u00adicles and news items sub\u00admit\u00adted using the stand\u00adard inter\u00adface behaved prop\u00aderly, it was just the news items sub\u00admit\u00adted by people without logins on the sys\u00adtem that didn\u2019t.<\/p>\n<p>It turns out that the prob\u00adlem lay in the way I called the <code>get_or_create()<\/code> meth\u00adod, which is <a href=\"https:\/\/docs.djangoproject.com\/en\/1.11\/ref\/models\/querysets\/#get-or-create\">used to look up the tags in the data\u00adbase and then cre\u00adate them if they don\u2019t exist<\/a>. In my code, that looked like&nbsp;this:&nbsp;<\/p>\n<pre>tag, create = Tag.objects.get_or_create(name=tag_name)<\/pre>\n<p>By default, this is a case-sens\u00adit\u00adive meth\u00adod (as it should be, for the gen\u00ader\u00adal case). To make the look\u00adup case-insens\u00adit\u00adive, you use <a href=\"https:\/\/docs.djangoproject.com\/en\/1.11\/ref\/models\/querysets\/#iexact\"><code>name__iexact<\/code><\/a> instead of <code>name<\/code>. The next prob\u00adlem I found was that no tags were being cre\u00adated if the tag did\u00adn\u2019t already exist in the data\u00adbase. To cre\u00adate the tag, if you\u2019re using <code>name__iexact<\/code> instead of <code>name<\/code> for the tag look\u00adup, you also need to give the <code>get_or_create()<\/code> meth\u00adod a defaults para\u00admet\u00ader to use when cre\u00adat\u00ading the tag. Now that line looks like&nbsp;this:<\/p>\n<pre>tag, create = Tag.objects.get_or_create(defaults={'name': tag_name},\n                                        name__iexact=tag_name)\n<\/pre>\n<p>and it all works the way it\u2019s meant&nbsp;to.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a while there <span class=\"caps\">XML<\/span>.com did\u00adn\u2019t handle tags on sub\u00admit\u00adted news items very well. If a tag was included that was in a dif\u00adfer\u00adent case to an exist\u00ading tag, the pre\u00adview and pub\u00adlish would res\u00adult in a 500 serv\u00ader error. For\u00adtu\u00adnately this was some\u00adthing that was\u00adn\u2019t vis\u00adible to the out\u00adside world, but annoy\u00ading non\u00adethe\u00adless. Wag\u00adtail \u2026 <a href=\"https:\/\/www.laurenwood.org\/anyway\/2017\/07\/tags-and-case\/\" class=\"more-link\">Con\u00adtin\u00adue read\u00ading<span class=\"screen-reader-text\"> \u201cTags and&nbsp;case\u201d<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wp_typography_post_enhancements_disabled":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":""},"categories":[112,114],"tags":[],"class_list":["post-1497","post","type-post","status-publish","format-standard","hentry","category-django","category-wagtail"],"_links":{"self":[{"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/posts\/1497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/comments?post=1497"}],"version-history":[{"count":4,"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/posts\/1497\/revisions"}],"predecessor-version":[{"id":1501,"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/posts\/1497\/revisions\/1501"}],"wp:attachment":[{"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/media?parent=1497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/categories?post=1497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laurenwood.org\/anyway\/wp-json\/wp\/v2\/tags?post=1497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}