def __str__(self):
"""
String for representing the Model object
"""
return '%s (%s)' % (self.id,self.book.title)
。
class Book(models.Model):
"""
Model representing a book (but not a specific copy of a book).
"""
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
# Foreign Key used because book can only have one author, but authors can have multiple books
# Author as a string rather than object because it hasn't been declared yet in the file.
summary = models.Text Field(max_length=1000, help_text="Enter a brief description of the book")
is bn = models.Char Field('ISBN',max_length=13, help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')
genre = models.Many To Many Field('Genre', help_text="Select a genre for this book")
# Many To Many Field used because genre can contain many books. Books can cover many genres.
# Genre class has already been defined so we can specify the object above.
def __str__(self):
"""
String for representing the Model object.
"""
return self.title
我正在使用Django框架的part Admin,当我想进入part Book实例时,它显示了这个错误:'None Type‘对象没有'title’属性,有人能告诉我为什么会发生这种情况吗?
发布于 2020-04-12 23:22:55
您的模型定义中有许多错误的空格。以下单词应写成不带空格:
TextField
isbn
CharField
ManyToManyField
我复制了代码,通过这些更正,它运行得很好。
https://stackoverflow.com/questions/61177879
复制