-
-
Notifications
You must be signed in to change notification settings - Fork 622
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
Usage - Making tags on a per user basis #809
Comments
Try using the TaggedItem instead of Tag for your query, it has a class method that returns a queryset of Tags for the object provided. Something like this should do it: from taggit.models import TaggedItem
tags = TaggedItem.tags_for(request.user) |
@CarloCogni Basically you're most of the way there, just call from the
here our custom tag model has an extra |
Would be good to add this to the FAQ, as multitenant keys is a super common thing that people need to deal with. |
Apologies for potentially hijacking this thread but I'm confused about the example referenced above and how it applies to OP's question. If two users attempted to use the same tags, wouldn't that result in a uniqueness validation error? Is this the sort of situation you were considering when suggesting the end user create their own models, @rtpg? For example:
If I modify the taggit test case referenced above in accordance with my example, the test fails with:
If a custom model was used and the uniqueness constraint was a compound one across name and user_id, would that just work with the surrounding taggit machinery? |
@ethagnawl thanks for the question, it does feel like we should document a full flow for this I think there are multiple ways to do this, here is a way that I handled this a while back: from taggit.models import TaggableManager, GenericTaggedItemBase
class MyTag(models.Model):
user = models.ForeignKey(...)
name = models.CharField(....)
class MyTaggedItem(GenericTaggedItemBase):
tag = models.ForeignKey(MyTag, ...)
class MyResource(models.Model):
# this allows me to put tags on the resource
tags = TaggableManager(blank=True, through=MyTaggedItem) The core point here is that I set up a tagged item model, but provide my own tag class entirely (thus being able to set my unique constraints myself instead of using the ones set in There might be a way to subclass |
Thanks for following up, @rtpg! That's pretty close to what I started sketching out yesterday after asking my question above. So far, so good but you're spot on about needing to lean on tests. I'm happy to potentially help with documentation once I'm satisfied with my solution. |
Hi,
in my project there is a user case where I need to make the tags associated with the user. This way every user can create their own set of tags, also when I present a page with all tags I could to something like Tag.objects.filter(user=request.user)
I've asked a question on StackOverflow, but I'm thinking I could be useful to ask you directly.
https://stackoverflow.com/questions/72823187/django-how-to-make-tags-on-a-per-user-basis.
Let me know if I can provide with more informations.
Thank you very much.
Cheers
Carlo
The text was updated successfully, but these errors were encountered: