Jul 232017
 

For a while there XML.com did­n’t handle tags on sub­mit­ted news items very well. If a tag was included that was in a dif­fer­ent case to an exist­ing tag, the pre­view and pub­lish would res­ult in a 500 serv­er error. For­tu­nately this was some­thing that was­n’t vis­ible to the out­side world, but annoy­ing nonetheless.

Wag­tail allows case-insens­it­ive tags, and I had already turned that on (it would be con­fus­ing to have searches for the tags “XSLT” and “xslt” return dif­fer­ent res­ults, for example). Art­icles and news items sub­mit­ted using the stand­ard inter­face behaved prop­erly, it was just the news items sub­mit­ted by people without logins on the sys­tem that didn’t.

It turns out that the prob­lem lay in the way I called the get_or_create() meth­od, which is used to look up the tags in the data­base and then cre­ate them if they don’t exist. In my code, that looked like this: 

tag, create = Tag.objects.get_or_create(name=tag_name)

By default, this is a case-sens­it­ive meth­od (as it should be, for the gen­er­al case). To make the look­up case-insens­it­ive, you use name__iexact instead of name. The next prob­lem I found was that no tags were being cre­ated if the tag did­n’t already exist in the data­base. To cre­ate the tag, if you’re using name__iexact instead of name for the tag look­up, you also need to give the get_or_create() meth­od a defaults para­met­er to use when cre­at­ing the tag. Now that line looks like this:

tag, create = Tag.objects.get_or_create(defaults={'name': tag_name},
                                        name__iexact=tag_name)

and it all works the way it’s meant to.

  One Response to “Tags and case”

  1. What a hor­rible déform­a­tion pro­fes­sion­elle on the part of the API author.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

/* ]]> */