我正在开发一些项目,该项目从rest API读取数据,然后反序列化数据以存储到数据库中。下面是模型和反序列化器
class ExchangeInfo(models.Model):
server_time = models.BigIntegerField()
timezone = models.CharField(max_length=20)
class ExchangeInfoSerializer(serializers.ModelSerializer):
server_time = serializers.IntegerField(source='serverTime')
timezone = serializers.CharField(max_length=20)
class Meta:
model = ExchangeInfo
fields = '__all__'
以下是我用于反序列化数据的代码
def load_exchange_info():
exchange_info = client.get_exchange_info()
from common.models import ExchangeInfo
from common.serializers import ExchangeInfoSerializer
exchange_info_serializer = ExchangeInfoSerializer(data=exchange_info)
exchange_info_serializer.is_valid()
print(exchange_info_serializer.errors)
is_valid总是如何返回False,错误如下。
{'server_time': ['This field is required.']}
关于这个问题的任何建议?
修改:
正确的序列化程序是:
class ExchangeInfoSerializer(serializers.ModelSerializer):
serverTime = serializers.IntegerField(source='server_time')
timezone = serializers.CharField(max_length=20)
class Meta:
model = ExchangeInfo
fields = ('serverTime', 'timezone')