'Django multiple tag field

I'm trying to find a good tutorial for django how to create multiple tags in a model.

For example:

class Tag(models.Model):
   name = models.CharField()

class Sample(models.Model):
   name = models.CharField()
   urlA  = models.CharField()
   urlB  = models.CharField()
   tagA = models.ManyToManyField(Tag)
   tagB = models.ManyToManyField(Tag)

I would like to display the tags as an input field and separate by ',' and split in the save method. So I'd like to see 2 different input for the 2 tag field.

If you have an easy way to do or know a good tutorial, please tell me! :)

Thank you guys!



Solution 1:[1]

Edit: you do not have to have the actual table sets over laid. You can generate any queryset you want to inn your views. Your url conf can be set up to display the detail view from multiple url. If i am still not understanding then please refine your question.

For having multiple anything tags categories your either going m21 or m2m. So when you create your tags you can add them one by one. Are you familiar with what the Django ORM has to offer with some of its admin functionality? Please give the documentation a good look through. Your approach to this problem is anything but reasonable. Not trying to rub you the wrong way I'm no genius. You would do something like so.

class Tag(models.Model):
    title = models.CharField(max_length=250, blank=True)
    slug = models.SlugField(blank=True

    class Meta:
        verbose_name = "tag"
        verbose_name_plural = "tags"
        ordering = ['title']

    @models.permalink
    def get_absolute_url(self):
     return "/tags/%s/" % self.slug

class Entry(models.Model):
    title = models.CharField(max_length=250, blank=True)
    body = models.TextField()
    tags = models.ManyToMany('Tag')
    slug = models.SlugField()


    @models.permalink
    def get_absolute_url(self):
        return "/blog/%s/" % self.slug

There's a little more code to be done for the EntryAdmin and the TagAdmin models, Many other things that can be done as well. I am not sure what you are trying to achieve with that if you could be more clear? Thank you, the above is a rough illustration of how I would approach it.

Solution 2:[2]

I found a solution from here:
https://dev.to/thepylot/how-to-add-tags-to-your-models-in-django-django-packages-series-1-3704

django-taggit is very useful for tagging.

It is

a reusable application that primarily offers you a Tag model, and a manager for easily adding tags to any model.

pip install django-taggit

After that, open settings.py and edit the installed apps section:

INSTALLED_APPS = [
    ...
    'taggit' 
]

After that, edit your model and add tags like this:

tags = TaggableManager()

The TaggableManager will show up automatically as a field in a ModelForm or in the admin.

Documentation: https://django-taggit.readthedocs.io/en/latest/index.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gino Mempin