在我的单元测试中,实际上,我创建的 ModelSerializer 类似乎丢弃了它提供的一整套数据:
class KeyboardSerializer(serializers.ModelSerializer):
user = serializers.Field(source='user.username')
mappings = KeyMapSerializer(many=True, source='*')
class Meta:
model = Keyboard
fields = ('user', 'label', 'is_primary', 'created', 'last_modified', 'mappings')
def pre_save(self, obj):
obj.user = self.request.user
TEST_KEYBOARD_MAP = {
'user': None,
'label': 'New',
'is_primary': True,
'created': '2013-10-22T12:15:05.118Z',
'last_modified': '2013-10-22T12:15:05.118Z',
'mappings': [
{'cellid': 1, 'key': 'q'},
{'cellid': 2, 'key': 'w'},
]
}
class SerializerTests(TestCase):
def setUp(self):
self.data = TEST_KEYBOARD_MAP
def test_create(self):
serializer = KeyboardSerializer(data=self.data)
print serializer.data
输出:
{'user': u'', 'label': u'', 'is_primary': False, 'created': None, 'last_modified': None, 'mappings': {'id': u'', 'cellid': None, 'key': u''}}
数据中传递给序列化程序的所有信息发生了什么?