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.

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/c2574ad9-e4cb-4b21-a255-8eb9db40ab9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to