In the code below, I have a book model, and I am trying to register that model along with the List Display with a decorator. Unfortunately, I am getting an error listed below, saying there is a TypeError with my Model List Display Class, where NonType object is not callable. I have looked into solutions, but haven't found any, so it would be nice to get some help. I am following MDN tutorial BTW, https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Admin_site. Thank you for helping!
Book Model
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
summary = models.TextField(max_length=1000, help_text='Enter a brief description of the book')
isbn = models.CharField('ISBN', max_length=13, unique=True,
help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn'
'">ISBN number</a>')
genre = models.ManyToManyField(Genre, help_text='Select a genre for this book')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('book-detail', args=[str(self.id)])
def display_genre(self):
return ', '.join(genre.name for genre in self.genre.all()[:3])
display_genre.short_description = 'Genre'
Book Model in Admin with List Display
@admin.site.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'display_genre')
Error
class BookAdmin(admin.ModelAdmin):
TypeError: 'NoneType' object is not callable
CodePudding user response:
Try this
def display_genre(self, book):
return ', '.join(genre.name for genre in book.genre.all()[:3])
(full disclosure: Not my fix. Seems this is an error in the tutorial and there has been a request to change it as noted in noted https://github.com/mdn/content/pull/16186/files)