Nameless
Nameless

Reputation: 519

Many-To-Many Fields View on Django Admin

Basically I am trying to recreate a fields in this model that is similar to django user authenticate system, for this I try using many-to-many fields but it ended up like this.

enter image description here

I am trying to have another field that can show what exist and another field for I have chosen similar to django admin, which looks like this.

enter image description here

This is my code

class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)

class BlogPage(models.Model):
    category = models.ManyToManyField(Category)
    title = models.CharField(max_length=128)
    preview = models.TextField(max_length=256)
    content = models.TextField()

Upvotes: 22

Views: 49377

Answers (2)

Neeraj Kumar
Neeraj Kumar

Reputation: 404

currently i am able to display many to many fields is admin panel

models.py

class Abc(models.Model):
    course = models.ManyToManyField(Course, blank=True)

admin.py

class AbcsAdmin(admin.ModelAdmin):
    """
    Admin panel management for Alumni
    """
list_display = ["id","get_course"]

def get_course(self,obj):
    return [course.name for course in obj.course.all()]

Upvotes: 5

souldeux
souldeux

Reputation: 3755

I believe what you want is a filter_horizontal widget used in the Admin template. This should help: Django Admin ManyToManyField

Upvotes: 39

Related Questions