[英]TypeError __str__ returned non-string (type int)
I'm trying to code a screenplay app, but I'm running into a problem when I actually add information to my model.我正在尝试编写一个剧本应用程序,但是当我实际将信息添加到我的 model 时遇到了问题。 My models look like this:
我的模型如下所示:
class Screenplay(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=100)
def __str__(self):
return self.title
class Block(models.Model):
class BlockTypes(models.IntegerChoices):
HEADING = 1, 'HEADING'
DESCRIPTION = 2, 'DESCRIPTION'
CHARACTER = 3, 'CHARACTER'
DIALOGUE = 4, 'DIALOGUE'
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
text = models.TextField()
type = models.PositiveSmallIntegerField(
choices=BlockTypes.choices,
default=BlockTypes.HEADING
)
screenplay = models.ForeignKey(Screenplay, on_delete=models.CASCADE, related_name='blocks')
def __str__(self):
return self.type
When I go to my backend api (localhost:8000/api/screenplays) I see my sample screenplays which resemble this:当我从 go 到我的后端 api (localhost:8000/api/screenplays) 我看到我的示例剧本类似于这样:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"uuid": "SAMPLE - UUID",
"title": "SAMPLE - TITLE",
"blocks": []
},
]
The error I posted in the title pops up when I actually add a block to a screenplay.当我实际向剧本添加一个块时,我在标题中发布的错误会弹出。 In my api it seems like
"blocks": []
but when I POST a block to a certain screenplay it breaks.在我的 api 中,它看起来像
"blocks": []
但是当我将一个块发布到某个剧本时,它会中断。 I just don't understand what the issue really is, if it's in my models.py or in some other part of my django app.我只是不明白问题到底是什么,如果它在我的 models.py 中或在我的 django 应用程序的其他部分中。 Do I need to add a blocks in my Screenplay model?
我需要在我的剧本 model 中添加块吗? Anyways, any advice would be great at this point.
无论如何,在这一点上,任何建议都会很棒。
__str__
must return string
and you are returning integer ie PositiveSmallIntegerField
. __str__
必须返回string
,并且您正在返回 integer 即PositiveSmallIntegerField
。 Convert that value to string
in your Block
model as将该值转换为
Block
model 中的string
def __str__(self):
return str(self.type)
More details onstr [Django-doc]有关str [Django-doc] 的更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.