Pra*_*ush 4 django django-models django-views django-serializer django-rest-framework
我正在学习有关 Django Rest Framework 的课程。我似乎已经逐字复制了代码,但是,似乎存在一些我无法归零的错误。什么可能导致此错误?
基本上,我正在测试模型序列化器。我正在尝试发布以下数据。
{
"id": 1,
"author": "John Doe",
"title": "Happy Birthday",
"description": "20 years of ISS",
"body": "Fancy content",
"location": "Earth",
"publication_date": "2020-06-11",
"active": false,
}
Run Code Online (Sandbox Code Playgroud)
我的序列化器类看起来:
class ArticleSerializer(serializers.ModelSerializer):
time_since_publication = serializers.SerializerMethodField()
class Meta:
model = Article
fields = '__all__'
def get_time_since_publication(self, object):
publication_date = object.publication_date
now = datetime.now()
time_delta = timesince(publication_date, now)
return time_delta
Run Code Online (Sandbox Code Playgroud)
我的模型是:
class Article(models.Model):
author = models.CharField(max_length=50)
title = models.CharField(max_length=120)
description = models.CharField(max_length=200)
body = models.TextField()
location = models.CharField(max_length=120)
publication_date = models.DateField()
active = models.BooleanField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.author} {self.title}"
Run Code Online (Sandbox Code Playgroud)
抛出的错误是 400 BAD REQUEST 错误:
{
"detail": "JSON parse error - Expecting property name enclosed in double quotes: line 13 column 1 (char 214)"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6580 次 |
| 最近记录: |