Отправить запрос на Schema Marshmallow + Django
У меня есть мнение :
class ArticlesListView(APIView):
authentication_classes = (JWTAuthentication,)
def post(self, request, *args, **kwargs):
"""Post an Article."""
try:
schema = ArticleSchema()
article = schema.load(
json.loads(request.body))
schema.context = {'request': request}
except ValidationError as e:
return json_response(e.messages, 400)
return json_response(schema.dump(article), 201)
which posts, хотя я хотел отправить запрос на ArticleSchema()
как я могу это сделать?
Ниже приведена схема :
class ArticleSchema(Schema):
class Meta(object):
model = Article
id = fields.Integer()
title = fields.String(validate=validate.Length(max=255))
content = fields.String()
regions = fields.Method(
required=False, serialize="get_regions", deserialize="load_regions"
)
@post_load
def update_or_create(self, data, *args, **kwargs):
print(" args ", args)
print(" kwargs ", kwargs)
regions = data.pop("regions", "")
title = data.pop("title", "")
content = data.pop("content", "")
user = User.objects.get(id=1)
article, _ = Article.objects.update_or_create(
id=data.pop("id", None),
owner=user,
title=title,
content=content,
defaults=data
)
if isinstance(regions, list):
article.regions.set(regions)
return article