我有一个项目模型,其中必须使用Django和DRF。因此,我正在制作一个不同的文件,该文件将从模型实例中获取所有对象并提供序列化。但是在django 1.9的情况下,我无法使用Model.serializer
from rest_framework import serializers
from slack.models import WebhookTransaction
from slack.message import Message
class WebhookTransactionSerializer(serializers.ModelSerializer)
class Meta:
model = WebhookTransaction
fields = '_all_'
class MessageSerializer(serializers.ModelSerializer)
class Meta:
model = Message
fields = '_all_'
运行服务器后,出现此错误
File "/Users/sid/webhook10/tutorial/slack/serializer.py", line 8
class MessageSerializer(serializers.ModelSerializer)
^
SyntaxError: invalid syntax
答案 0 :(得分:0)
如评论中的wmorrell
类定义必须以:结尾,并且以下定义需要缩进。
在序列化程序类定义之后添加分号,并在其后缩进代码,如下所示
from rest_framework import serializers
from slack.models import WebhookTransaction
from slack.message import Message
class WebhookTransactionSerializer(serializers.ModelSerializer):
class Meta:
model = WebhookTransaction
fields = '_all_'
class MessageSerializer(serializers.ModelSerializer):
class Meta:
model = Message
fields = '_all_'