django-siteflags icon indicating copy to clipboard operation
django-siteflags copied to clipboard

Best way to get a queryset with flagged objects of a certain model?

Open erwinelling opened this issue 3 years ago • 1 comments

I was hoping to do a quick check here. If I were to populate a queryset with objects of a certain type (in this case Profile), that were flagged a specific flag (in this case FLAG_STAR) by a specific user, would this be the best way to go?

flags_for_types = ModelWithFlag.get_flags_for_types([Profile], user=self.request.user, status=Profile.FLAG_STAR)            
qs = Profile.objects.filter(flags__in=flags_for_types[Profile])

erwinelling avatar May 10 '21 11:05 erwinelling

You may want to try various approaches to see what plays the best. For example use db_queries fixture in your tests to see what SQL commands are produced for your approach and compare those to, say, Profile.get_flags() with selected related linked_object. Or you may use django-debug-toolbar to see those SQLs and timings right in your browser.

idlesign avatar May 11 '21 02:05 idlesign