Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag Ordering Inconsistent #870

Open
steverecio opened this issue Oct 8, 2023 · 3 comments · May be fixed by #871
Open

Tag Ordering Inconsistent #870

steverecio opened this issue Oct 8, 2023 · 3 comments · May be fixed by #871

Comments

@steverecio
Copy link

Looking at the docs, it appears that the tags.all() queryset should maintain the ordering of the tags as they were initially set. However, in testing this does not hold. The tags do not in fact maintain their original ordering. How can I retrieve the tags with their original ordering intact?

Please note the sample below which shows that the ordering is not maintained as the docs show.

>>> apple = Food.objects.create()
>>> apple
<Food: Food object (1)>
>>> apple.tags.add("red", "green", "delicious")
>>> apple.tags.all()
<QuerySet [<Tag: green>, <Tag: red>, <Tag: delicious>]>
@steverecio
Copy link
Author

The issue seems to stem from the use of unordered sets here: https://github.com/jazzband/django-taggit/blob/master/taggit/managers.py#L196

@steverecio steverecio linked a pull request Oct 11, 2023 that will close this issue
@rtpg
Copy link
Contributor

rtpg commented Oct 24, 2023

Looking at the docs, it appears that the tags.all() queryset should maintain the ordering of the tags as they were initially set.

did you see that anywhere? Or is this just what you saw in the docs

Basically my feeling here is that Manager has an ordering property in the description. If you care about the insertion order, you can set this to pk. If you don't then we let the DB do what it wants.

Having said that... the change you sent in the PR makes it seem like we're dealing with a slightly different issue (where when we are creating them, the cached model instance list doesn't match the passed in values). I don't know what to think of that.

If anything I feel like the default ordering of name would make more sense? Though that ordering is less obvious in non-latin languages.


Anyways, questions I'm mulling over:

  • should we just set a default ordering?
    • should that be by order of creation of the tags
    • should that be by name? (=> need to make sure this code doesn't break with custom tags that don't have a name)
  • How can we do this in a way that doesn't accidentally break existing people's setups?

@steverecio
Copy link
Author

steverecio commented Oct 25, 2023

did you see that anywhere? Or is this just what you saw in the docs

The readme indicates this is the default behavior. This intuitively makes the most sense to me. If I add tags in a particular order, I would expect them to be returned in that same order. For example, on our site we have a form where you can add a set of tags and save. However, after saving, the order of tags is unpredictable.

The problem is the manager reorders the tag list by adding them to a set which removes the agency of the developer to impose their own desired ordering. If you wanted to impose any particular order (tag creation date, tag name, etc) then that sorting can be handled prior to calling mymodel.tags.set(...my_tag_list). Similarly, if I iterated over a list and called mymodel.tags.add(tag) for each tag, I would expect that order to be respected when I retrieve the results.

I think the possible tag orderings are the following:

  • Creation date of the tag (requires that they are actually created in the order they are passed in)
  • Name of the tag
  • Order at which the tags were added to the target object (my personal preference). This is how django handles many-to-many relations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants