jazzband / django-taggit

Simple tagging for django
https://django-taggit.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.34k stars 622 forks source link

Create Tag without a foreign-key'd model #835

Closed pancakeDevelopment closed 1 year ago

pancakeDevelopment commented 1 year ago

Hi, is there a way to create indepentend tags like:

Tag.objects.create(name="Tag name", slug="tag-name")

Reason: In my use case are allowed to create tags an users can use the pre-defined tags. So users should not be allowed to create new ones.

rtpg commented 1 year ago

Yes! So like you said, what you need to do is something like Tag.objects.create(...) to first create the tags (this is if you are using the base Tag model). So you can just do from taggit.models import Tag and go from there.

Now the rest is a bit manual but here's an idea for how I would do this from scratch. Here I'm assuming you add tags to a Post model, via post.tags.

You can also create a special Tag subclass with some extra validation to ensure that the tag name is correct. What makes sense depends on what you are doing, really.