我正在尝试通过管理员保存模型,但我不断收到此错误
unsupported operand type(s) for +: 'DeferredAttribute' and 'str'".
我想返回模型的字符串表示形式。
这是我尝试过的:
def __str__(self):
return str(self.album_name + "" + self.artist)
我的模型:
class Album(models.Model):
album_name = models.CharField(max_length=250,)
album_logo = models.FilePathField(path='music/static/music/song_logo',
null=True, blank=True, recursive=True)
artist = models.CharField(max_length=150)
primaryid = models.AutoField(primary_key=True, null=False)
@classmethod
def __str__(self):
return self.album_name + " " + self.artist
例如,我想要像“hilltop”这样的东西
最佳答案
__str__
不应该是 classmethod
,删除该装饰器将解决您的问题。
您将类 (Album
) 作为 self
而不是实例传递给 __str__
方法。请参阅this question了解详情。
关于python - + : 'DeferredAttribute' and 'str' 不受支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55409044/