pythondjangotaggingdjango-tagging

How to allow whitespaces in tag names using django-tagging-0.3.1?


I'm using the django-tagging-0.3.1 package to tag all the articles in my website. But I just found that this package seems not to support whitespace in tag name, which means when a tag name has whitespace in it and you use the function it provides, TaggedItem.objects.get_by_model, to search all the articles with this tag, you will get an empty list. But actually I checked my server and the tag name is shown and associated with the corresponding articles correctly. So is there any way I can configure to make the space-contained-tag search work? Thanks.


Solution

  • The problem appears to be in the get_tag_list utility function, which is called by get_by_model. If passed a string (such as 'my tag with spaces'), it will return the following query:

    Tag.objects.filter(name__in=parse_tag_input(tags))
    

    Unfortunately, the parse_tag_input function assumes that space-separated words are different tags, so it will assert that you are searching for the tags ['my', 'tag', 'with', 'spaces'], which will of course not return anything.

    Later in the get_tag_list function, there's a check to see if the input is a list/tuple:

    elif isinstance(tags, (types.ListType, types.TupleType)):
        ...
    

    The code following it does not call parse_tag_input (I guess it assumes that each string within the list/tuple is a fully-formed tag).

    TL;DR: Thus, I believe that if you pass a tuple or a list containing the tag name, it will correctly find it.

    TaggedItem.object.get_by_model(MyModel, ['my tag with spaces'])