I'm trying to code a screenplay app, but I'm running into a problem when I actually add information to my 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:
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. 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. Do I need to add a blocks in my Screenplay model? Anyways, any advice would be great at this point.
__str__
must return string
and you are returning integer ie PositiveSmallIntegerField
. Convert that value to string
in your Block
model as
def __str__(self):
return str(self.type)
More details onstr [Django-doc]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.