Django的串行查詢數據庫序列化的幾個對象使用Django序列化,但問題是每個序列化查詢從數據庫中foerign鑰匙的ID,而不是從對象例如只是把它爲forign密鑰ID
class QBAccount(CompanyModel):
company = models.ForeignKey(Company)
>>> from deretoapp.models import QBAccount
>>> import logging
>>> l = logging.getLogger('django.db.backends')
>>> l.setLevel(logging.DEBUG)
>>> l.addHandler(logging.StreamHandler())
>>> a = QBAccount.allobjects.all()[0]
>>> from django.core import serializers
>>> serializers.serialize('python', [a])
(0.000) SELECT `deretoapp_company`.`id`, ... FROM `deretoapp_company` WHERE `deretoapp_company`.`id` = 45995613-adeb-488f-9556-d69e856abe5f ; args=(u'45995613-adeb-488f-9556-d69e856abe5f',)
[{'pk': u'3de881eb-8409-4089-8de8-6e24f7281f37', 'model': u'deretoapp.qbaccount', 'fields': {... 'company': u'45995613-adeb-488f-9556-d69e856abe5f' ....}}]
有沒有辦法更改,恕不修改Django的代碼這種行爲?我知道a.company.id
將查詢公司表(這不應該在理想的世界中發生的),但有在串行的選項,以便它像a.company_id
,不會查詢數據庫
>>> django.VERSION
(1, 3, 1, 'final', 0)
這將無法避免數據庫查詢,我想'ID即pk'不自然鍵 – 2012-01-27 16:38:48