Ticket #9394: pk-trace.diff

File pk-trace.diff, 1.6 KB (added by Alex Gaynor, 16 years ago)

optimization, only tested on forward relations, but it's a 2 query optimization for this case, all tests pass

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index b763b05..6c117f4 100644
    a b class RelatedField(object):  
    128128            # down until we hit a value that can be used for a comparison.
    129129            v, field = value, None
    130130            try:
    131                 while True:
    132                     v, field = getattr(v, v._meta.pk.name), v._meta.pk
     131                v, field = getattr(v, v._meta.pk.attname), v._meta.pk
    133132            except AttributeError:
    134133                pass
    135134            if field:
  • tests/regressiontests/model_inheritance_regress/models.py

    diff --git a/tests/regressiontests/model_inheritance_regress/models.py b/tests/regressiontests/model_inheritance_regress/models.py
    index 06a886e..c006e5f 100644
    a b DoesNotExist: ArticleWithAuthor matching query does not exist.  
    267267>>> fragment.find('pub_date', pos + 1) == -1
    268268True
    269269
     270>>> from django.conf import settings
     271>>> settings.DEBUG = True
     272>>> from django.db import connection, reset_queries
     273>>> ir = ItalianRestaurant.objects.create(serves_gnocchi=True, serves_pizza=True, serves_hot_dogs=False, name="Django Restaurant")
     274>>> _ = Supplier.objects.create(restaurant=ir)
     275>>> ir = ItalianRestaurant.objects.get(pk=ir)
     276>>> reset_queries()
     277>>> connection.queries
     278[]
     279>>> Supplier.objects.filter(restaurant=ir)
     280[<Supplier: Supplier object>]
     281>>> len(connection.queries)
     2821
     283>>> settings.DEBUG = False
    270284"""}
Back to Top