Passing ordering parameter to the TaggableManager
See original GitHub issueSo far I am doing it like this:
class _SortedTaggableManager(_TaggableManager):
def get_queryset(self, *args, **kwargs):
qs = super(_SortedTaggableManager, self).get_queryset(*args, **kwargs)
return qs.order_by("name")
Then later in the code I am defining the following:
tags = TaggableManager(manager=_SortedTaggableManager)
But I feel it would be much easier to simply write:
tags = TaggableManager(ordering=["name"])
What do you think?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Passing ordering parameter to the TaggableManager #370
I'm currently struggling with custom tags and related models not honoring the ordering option the Meta class. In my case, I'm tagging things ......
Read more >django-taggit - Read the Docs
your content model named content_object. Pass this intermediary model as the through argument to. TaggableManager:.
Read more >The API — django-taggit 1.3.0 documentation - Read the Docs
Returns a list (not a lazy QuerySet ) of other objects tagged similarly to this one, ordered with most similar first. Each object...
Read more >django-taggit get number of posts that use a particular tag
I tried to look into the source code of django-taggit, specifically the TaggableManager, to see if I can override something there and get...
Read more >Recipes — Wagtail Documentation 4.1.1 documentation
Wagtail defaults to serving Page -derived models by passing a reference to the page object to a Django HTML template matching the model's...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just throwing this out there for anyone who comes across this looking to solve
prefetch_related
query issues.The taggable manager has a workaround in the
get_queryset
that helps avoid spawning a ton of queries. Removing that causes a specifiedprefetch_related
to both send the prefetch query and then still fetch tags on each instance.Here’s a version of @Natim’s manager that supports
prefetch_related
. I’ve submitted a version of this as a PR.@rtpg Thanks for the reply. Apologies for the delay in getting back with you. To be fair, I implemented this a while back so the code is not fresh, but this is what I’ve got (I could totally be doing this wrong):
I then do something like this in the template (it’s a
ListView
forRN
objects):This spits out the list of RNs and associated tags but the order is mixed.
Here is the RN model (some bits omitted):
I should note that I’ve tried setting the
ordering
on all the models included in this relationship to try and coax out the tag order, but nothing seemed to work. If you have any ideas on a workaround, that would be awesome. As a total hack, I have anorder
field on the base class from which all my models inherit (not the custom tag, but my own models) so I’m using this to set the order ofRN
objects.Also, perhaps what is making this trickier than expected is that
RN
has aForeignKey
relationship to another model,PR
. So myListView
above is actually forPR
. I’m doing this because I need to loop throughPR
s first, then for eachPR
, I need to loop through all the associatedRN
s (and thustags
, cause eachRN
has a single tag).