django-taggit
django-taggit copied to clipboard
Further queryset filter parameter for similar_objects method
Hi, as far as I can see the current implementation of TaggableManager::similar_objects lacks the ability to specify further (exclude) filters for the result set. Thanks for investigating this issue.
That's correct. As the documentation (http://packages.python.org/django-taggit/api.html#similar-objects) says, this method returns a list, not a queryset. I don't think there's anything we can do about this.
I haven't thought of anything, so I'm closing this.
sorry for being late to explain my feature request.
The current implementation requires me to call similar_objects once to get a list of all objects which look the same according to their tags. When I want to apply further filter on that result set, I need to get the IDs of all returned objects first and then do a second query with my filters and the ID list.
I owe you a beer if you could improve the similar_objects method to accept a filter queryset that applies on the queryset to retrieve similar objects ;D.
I've reopened this to think about it some more.
Bump I really don't want to hurry you since a good job needs the time it requires but I need that feature soon and I believe you can do the job much better here then me.
I have a branch of Taggit in which I have implemented what seems to be a simple fix for this problem.
Here is how my similar_objects
method looks:
def similar_objects(self, num=None, **filters):
lookup_kwargs = self._lookup_kwargs()
lookup_keys = sorted(lookup_kwargs)
qs = self.through.objects.values(*lookup_kwargs.keys())
qs = qs.annotate(n=models.Count('pk'))
qs = qs.exclude(**lookup_kwargs)
subq = self.all()
qs = qs.filter(tag__in=list(subq))
qs = qs.order_by('-n')
if filters is not None:
qs = qs.filter(**filters)
if num is not None:
qs = qs[:num]
// etc... (the rest is the same)
Now a user can easily specify both a count and any filter methods that they need when doing a similar_objects
query.
I'd love to have some extra filtering in place.
Right now I'm writing a FAQ app, and would love to have it return "similar questions". That means creating a "through table" right now, because otherwise other objects will also be returned.
+1 on this.
+1, perhaps order_by would be good too
+1
Also, the ability to retrieve only objects of a specific given model would be great!
+1
FYI, this is what I've eventually written back then: https://github.com/django-fluent/django-fluent-utils/blob/master/fluent_utils/softdeps/taggit.py#L73 this code could clearly be optimized, but it could be a good starting point.
#709 is a dupe of this issue
looking at this now (after many extra years), I think it might be possible for us to solve this. Django's ORM has made many strides, though the query itself might end up being extra gnarly