Django REST:如何更改ModelSerializer类的默认字段类型

时间:2018-11-20 18:29:55

标签: python django api django-rest-framework

我正在尝试覆盖串行器的data字段,例如CharField

from jsonfield import JSONField

class ActivitySerializer(serializers.ModelSerializer):
    data = JSONField()
    class Meta:
        model = Activity
        fields = ("date_added", "start_date", "data", "number", "athlete")

但是当我尝试打印序列化器的表示形式-print(repr(my_serializer))时,字段data的类型保持不变:

ActivitySerializer(<Activity: START DATE: 2018-11-19 06:08:49+00:00, ID: 1973222369, DATE ADDED: 2018-11-20 18:07:32.818798+00:00>):
    date_added = DateTimeField(read_only=True)
    start_date = DateTimeField(required=False)
    data = CharField(style={'base_template': 'textarea.html'})
    number = IntegerField(required=False)
    athlete = PrimaryKeyRelatedField(queryset=Athlete.objects.all(), required=False)

我在做什么错了?

0 个答案:

没有答案