Задать вопрос

Тел: +7 965 3737 888

499

Просмотров

2

Ответов

Prefill ForeignKey caches

<p>Provides an efficient means of looking up multiple related model instances for a range of objects by pre-filling the cache attribute used by SingleRelatedObjectDescriptor with either complete model instances or a dict containing only specified fields, looking up all required data with a single query.</p>
<p>Example usage:</p>
C:\django_projects\soclone&gt;django-admin.py shell
&gt;&gt;&gt; from soclone.models import Question
&gt;&gt;&gt; from soclone.views import populate_fk_caches
&gt;&gt;&gt; from django.db import connection
&gt;&gt;&gt; from django.contrib.auth.models import User
&gt;&gt;&gt; q = Question.objects.get(id=1)
&gt;&gt;&gt; a = list(q.answers.all())
&gt;&gt;&gt; connection.queries = []
&gt;&gt;&gt; populate_fk_caches(User, (
...         ((q,), ('author', 'last_edited_by', 'closed_by')),
...         (a,    ('author', 'last_edited_by')),
...      ),
...      fields=('username', 'gravatar', 'reputation', 'gold', 'silver',
...              'bronze'))
&gt;&gt;&gt; connection.queries
[{'time': '0.000', 'sql': u'SELECT "auth_user"."id", "auth_user"."username", "au
th_user"."gravatar", "auth_user"."reputation", "auth_user"."gold", "auth_user"."
silver", "auth_user"."bronze" FROM "auth_user" WHERE "auth_user"."id" IN (1, 2)'
}]

Вопрос полезен? Да0/Нет0
file_3570.py(2.8Кб)
None

Ответы (2):

Ответanentropic:24.08.2011
Ответ полезен? Да0/Нет0

also, for the '%s_id'

class RelatedObject(object):
    get_attname()

Ответanentropic:24.08.2011
Ответ полезен? Да0/Нет0

In recent versions of Django the "_%s_cache" string shouldn't be hard-coded as there's a method to get it

db/models/related.py

class RelatedObject(object):
    get_cache_name()