Opened 9 years ago

Closed 9 years ago

#17602 closed Bug (fixed)

Django serializer does unnecessary db queries

Reported by: anuraguniyal Owned by: Grzegorz Nosek
Component: Core (Serialization) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When django serializes object, to get the foreign key id referenced in object it queries the db instead of just using the id e.g.

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' ....}}]

Here db query after serializers.serialize is not needed because compay id could have been obtained directly by a.company_id, and query itself shows id is being passed to db to get the id, it looks like problem is because is a db query which in ideal world also should not happen.

Attachments (1)

17602.diff (3.9 KB) - added by Grzegorz Nosek 9 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 9 years ago by Grzegorz Nosek

Component: UncategorizedCore (Serialization)
Owner: changed from nobody to Grzegorz Nosek
Status: newassigned

Changed 9 years ago by Grzegorz Nosek

Attachment: 17602.diff added

comment:2 Changed 9 years ago by Grzegorz Nosek

Has patch: set
Triage Stage: UnreviewedAccepted

comment:3 Changed 9 years ago by neaf

Triage Stage: AcceptedReady for checkin

comment:4 Changed 9 years ago by Jannis Leidel

Resolution: fixed
Status: assignedclosed

In [17439]:

Fixed #17602 -- Stopped the XML serializer from doing unneeded queries. Thanks, gnosek.

Note: See TracTickets for help on using tickets.
Back to Top