当我导航到此模型贷款的管理页面或通用视图时,我收到以下错误:
File "library_app/catalog/models.py", line 118, in __str__
return f'{self.book.title} ({self.id})'
AttributeError: 'NoneType' object has no attribute 'title'
我设法找到了我的BookInstance类的__str__
函数.
class Loan(models.Model):
book_instance = models.OneToOneField(BookInstance, on_delete=models.RESTRICT)
user = models.ForeignKey(User, on_delete=models.RESTRICT)
date = models.DateField(
default=django_timezone.now,
help_text='Date borrowed'
)
due_back = models.DateField(
default= django_timezone.now() + datetime.timedelta(30),
help_text='Date due to be returned'
)
returned_date = models.DateField(null=True, blank=True, help_text='Date book was returned')
class BookInstance(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
book = models.ForeignKey(Book, on_delete=models.RESTRICT, null=True)
LOAN_STATUS = (
('m', 'Maintence'),
('o', 'On loan'),
('a', 'Available'),
('r', 'Reserved'),
)
status = models.CharField(
max_length=1,
choices=LOAN_STATUS,
blank=True,
default='m',
help_text='Book Availability'
)
def __str__(self):
return f'{self.book.title} ({self.id})'
我猜在这些页面中,当__str__
方法被调用时,使用的是错误的self
变量.我不知道如何解决这个问题,所以任何提示都将不胜感激.