I have a queryset containing some objects. Depending on some case or the other i now want to exclude all the objects without certain tags (_tags is the name of the TagField on my model):
self.queryset=self.queryset.exclude(_tags__id__in=avoid)
But this just leaves me with an error:
Caught FieldError while rendering:
Join on field '_tags' not permitted.
Did you misspell 'id' for the lookup type?
As i’m pretty sure i did not misspell ‘id’, i did some searching on how to use tagging for something like this. In the docs there is a lot about custom Managers, but somehow i just can’t get it how i can use them to get what i want.
edit:
corrected the code above to
self.queryset=self.queryset.exclude(_tags__in=avoid)
where avoid is a list of integers. And that leaves me with the problem that the TagField of django-tagging is just a special CharField (or TextField?). Which will, of course, not sort out anything if i just query it against a list of integers. I could try to solve this in a way like this:
for tag in avoid:
self.queryset=self.queryset.exclude(_tags__contains=tag.name)
which is not only ugly, but also leaves me with the problem of tags made of multiple words or matching parts of other tags.
I somehow have the suspicion that this could be solved in a much prettier way by someone who has understood how django-tagging works.
As described in the comment on Chris’ answer, django-tagging does not deliver the tagstring when accessing
model._tag. In the end i had no other solution than to do the query and sort out the loops containing a certain tag afterwards:To complete that the model for this looks like this:
Allthough i tried for quite a while, i found no way of chaining a query against tagging tags into a query chain for an object. For this project I’m stuck with tagging, but this is a real drawback…