简体   繁体   English

MDN Django TypeError: 'NoneType' object 不可调用

[英]MDN Django TypeError: 'NoneType' object is not callable

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.在下面的代码中,我有一本书 model,我正在尝试使用装饰器将 model 与列表显示一起注册。 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.不幸的是,我收到下面列出的错误,说我的 Model 列表显示 Class 存在 TypeError,其中 NonType object 不可调用。 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 .我正在关注 MDN 教程 BTW, https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Admin_site Thank you for helping!谢谢你的帮忙!

Book Model预定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在后台预订Model 带列表显示

@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

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 ) (完全披露:不是我的解决方法。这似乎是教程中的错误,并且已请求更改它,如https://github.com/mdn/content/pull/16186/files中所述)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM