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

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

Closed
pancakeDevelopment opened this issue Nov 21, 2022 · 1 comment
Closed

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

pancakeDevelopment opened this issue Nov 21, 2022 · 1 comment
Labels

Comments

@pancakeDevelopment
Copy link

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 rtpg added the question label Nov 21, 2022
@rtpg
Copy link
Contributor

rtpg commented Nov 21, 2022

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.

  • In the code that creates Posts, write a validation that checks that the provided tags all already exist (Tag.objects.filter(name__in=tag_list) == len(tag_list), if users are passing in a tag list as strings). If the length is mismatched, that means that in the tag list there is a tag that doesn't exist already
  • Create an admin page for Tags or the like so that you can create tags. Alternatively, programmatically create tags inside of a data migration

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.

@rtpg rtpg closed this as completed Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants