Please I need help on how to create a custom method for a model with list_display

22 views
Skip to first unread message

Godson Rapture

unread,
May 19, 2018, 2:54:17 PM5/19/18
to Django users

Please I am learning Django 2.0.4


Right now I am learning about Django Admin.


I got an error that the value of my list_display should not be a ManyToMany Field.


I referred back to Django official doc and I was asked to create a custom method for my model.


I tried looking for an example of how to do that, I could not see.


Here is my model.py program

 from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50,blank=True)
    city = models.CharField(max_length=60,blank=True)
    country = models.CharField(max_length=50,blank=True)
    website = models.URLField()
    def __str__(self):
        return self.name

class Author(models.Model):
    salution = models.CharField(max_length=10)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    headshot = models.ImageField(upload_to = "tmp")
    def __str__(self):
        return f"{self.first_name} {self.last_name}"

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher,on_delete=models.CASCADE,)
    publication_date = models.DateField()
    def __str__(self):
        return self.title

Here is my admin.py program

from django.contrib import admin
from polls.models import Book, Publisher, Author
# Register your models here.

class BookAdmin(admin.ModelAdmin):
    list_display = [
        "title",
        "authors", #a ManyToMany Field

    ]
     search_fields = [
        "Book"
    ]
    class Meta:
        model = Book

admin.site.register(Book, BookAdmin)

Can someone please help me.

Thanks.

Daniel Germano Travieso

unread,
May 19, 2018, 10:57:12 PM5/19/18
to Django users
Hello!

On your case, as stated on the official doc, you should just create a method on the model you want to display the ManyToMany field on the list_display that maybe returns the list of items from the ManyToMany field. Django avoids using this as it could be a potential overhead as you will run seperate queries on the database to fetch all the related many-to-many objects.

But if you are feeling adventurous, just create the method that performs that query.

i.e. On your Book model you create a method that returns the self.authors.all() set. Then you use that method's name on the list_display instead of authors.

Hope it helps.

Godson Rapture

unread,
May 20, 2018, 5:42:42 AM5/20/18
to django...@googlegroups.com
Thanks very much. I have solved the problem. 
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/56c58fb6-7074-47aa-9536-8d8027ef70a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages