How to access a ManyToManyField

from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField

class Topic(models.Model):
    about = models.CharField(max_length=30)
    def __str__(self):
        return self.about

class Blog(models.Model):
    title = models.CharField(max_length=30)
    date_added = models.DateTimeField()
    about = models.CharField(max_length=30)
    topics = models.ManyToManyField(Topic)
    content = RichTextUploadingField()
    def __str__(self):
        return self.title

How can I show “topics”(a ManyToMnayField)?


can you elaborate a bit? For example where do you want to show the topics? Generally in template you would access them like this blog.topics.all. You need the all part to be able to iterate over them.

1 Like

thank you for solving my question!