当我执行查询时,我能够访问正确的字段('author.username')。
但是,如果我通过序列化程序运行相同的查询,它不会打印出 select_related 模型字段。
我应该指定所有字段以及外键模型的附加字段吗?
视图.py
query = Chat.objects.select_related('author').filter(id=chat.id)
print(query) # [<Chat: 108>]
print(query[0]) # 108
print(query[0].author.username) # superman
context = serializers.serialize("json", Chat.objects.select_related('author').filter(id=chat.id))
print("---------")
print(context) # [{"model": "chats.chat", "pk": 108, "fields": {"author": 29, "text": "this is a message", "chatroom": 11, "written_at": "2016-01-07T23:03:28.968Z"}}]
--- 更新 --- views.py
context = ChatSerializer(chat)
# output
{u'text': u'come on', u'chatroom': OrderedDict([(u'id', 11), ('created_at', u'2016-01-07T00:27:28.391467Z'), ('creator', 29), ('participant', 1)]), u'written_at': u'2016-01-08T01:12:20.310776Z', u'author': OrderedDict([(u'id', 29), ('password', u'pbkdf2_sha256$24000$h0whlzx0BleT$4cUWdRkUsPYB7Ia6F+pYxX+31BMhJJfpWiLHfoL9NOY='), ('last_login', u'2016-01-07T00:26:37.907507Z'), ('email', u'superman@gmail.com'), ('username', u'superman'), ('first_name', u'clark'), ('last_name', u'kent'), ('is_active', True), ('is_admin', False), ('joined_on', u'2016-01-06T23:57:43.191484Z'), ('is_online', False), ('is_contractor', False), ('profile_pic', '/media/bicycle_z3NDBPI.jpeg'), ('braintree_id', u'20401140'), ('braintree_client_token', u'eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiI1MjIxM2UwZDQ4OGM0MmFkNTYzY2IzMDJlMDBkMmIyMWIwZWEzYzBiNWEzZTg2ZmNhMmJlNWM3MTI5YjNmNGJifGNyZWF0ZWRfYXQ9MjAxNi0wMS0wNlQyMzo1Nzo0NC42MTgzNzQ2NTgrMDAwMFx1MDAyNmN1c3RvbWVyX2lkPTIwNDAxMTQwXHUwMDI2bWVyY2hhbnRfaWQ9ZGZ6amRrdHM1ZnNxeXF0alx1MDAyNnB1YmxpY19rZXk9ZGtta245cDNj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WVyY2hhbnQiOmZhbHNlLCJicmFpbnRyZWVDbGllbnRJZCI6Im1hc3RlcmNsaWVudDMiLCJiaWxsaW5nQWdyZWVtZW50c0VuYWJsZWQiOnRydWUsIm1lcmNoYW50QWNjb3VudElkIjoia2FsbGFyb28iLCJjdXJyZW5jeUlzb0NvZGUiOiJVU0QifSwiY29pbmJhc2VFbmFibGVkIjpmYWxzZSwibWVyY2hhbnRJZCI6ImRmempka3RzNWZzcXlxdGoiLCJ2ZW5tbyI6Im9mZiJ9'), ('payment_method_nonce', u'dbd01080-35e2-4a3b-a695-560491a14567'), ('payment_method_token', u'cfty3w')])}
console.log() 输出
ChatSerializer(<Chat: 118>):
author = NestedSerializer(read_only=True):
id = IntegerField(label='ID', read_only=True)
password = CharField(max_length=128)
last_login = DateTimeField(allow_null=True, required=False)
email = EmailField(max_length=255, validators=[<UniqueValidator(queryset=User.objects.all())>])
username = CharField(max_length=80)
first_name = CharField(max_length=45)
last_name = CharField(max_length=45)
is_active = BooleanField(required=False)
is_admin = BooleanField(required=False)
joined_on = DateTimeField(read_only=True)
is_online = BooleanField(required=False)
is_contractor = BooleanField(required=False)
profile_pic = ImageField(allow_null=True, required=False)
braintree_id = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
braintree_client_token = CharField(allow_blank=True, allow_null=True, max_length=2000, required=False)
payment_method_nonce = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
payment_method_token = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
text = CharField(max_length=255)
chatroom = NestedSerializer(read_only=True):
id = IntegerField(label='ID', read_only=True)
created_at = DateTimeField(read_only=True)
creator = PrimaryKeyRelatedField(queryset=User.objects.all())
participant = PrimaryKeyRelatedField(queryset=Contractor.objects.all())
written_at = DateTimeField(read_only=True)