1

I need to add tags to my blog posts. Each tag should be available on many posts and also posts can have multiple tags.

How can I change the admin sites so that I can add multiple (existing) tags to a post? The standard view only lets me add by creating new ones.

model.py

# blog post tags
class Tag(models.Model):
    name = models.CharField(max_length=20)
    slug = models.SlugField(max_length=40, unique=True)
    date_created = models.DateTimeField(default=timezone.now)

    class Meta:
        ordering = ['name']

    def __str__(self):
        return self.name

# blog posts
class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    last_modified = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE) # 1 author per post
    tags = models.ManyToManyField(Tag, related_name='tags') # n tags per m posts

    class Meta:
        ordering = ['title']

    def __str__(self):
        return self.title

Current Admin Site

I know I need to edit my admin.py file in my blog application but everything i tried so far did not work. Is there a recipe for these admin views?

I want to achieve something like this (1st answer - filtered view).

0 Answers0