byteflow
view apps/lib/db.py @ 1363:f75ca7dfb4f0
fix py2.4 compat
| author | Alexander Solovyov <piranha@piranha.org.ua> |
|---|---|
| date | Wed Feb 03 11:48:39 2010 +0200 (5 weeks ago) |
| parents | 291c40de7660 |
| children |
line source
1 # -*- mode: python; coding: utf-8; -*-
3 """DB and Django ORM utilities and extensions."""
26 pass
34 """Get less possible number of inclusive ranges of integers keys."""
52 """Return WHERE statement for the ranges"""
64 def load_generic_related(object_list, related_qs, cache_field, field='object_id', ct_field='content_type'):
72 """Load backward related objects for a list of objects.
74 Function will do only 1 SQL query instead of len(object_list) queries.
76 Parameters:
78 - object_list - list of objects to attach related objects.
79 - related_qs - queryset (or a manager) from where to fetch related
80 objects.
81 - cache_field - name for attaching related objects, optional (determined
82 by the name of object_list model).
83 - field - relation field, by which related_qs is related to object_list,
84 optional (determined by the name of related_qs model).
85 """
94 # get related queryset
100 # generate list of related objects
104 # append related objects to cache field
113 """Load content objects for a generic relation.
115 Number of sql queries is equals not to number of objects but number of
116 unique content types. For example we have article and project models that
117 can be tagged and we want to load all objects tagged with a tag, then we
118 always will spent 2 SQL queries instead of number of tagged objects SQL
119 queries.
121 Parameters:
123 - object_list - list of objects with GenericForeignKey to process
124 - cache_field - name of attribute, which should contain loaded
125 object. Typically it's a name of a GenericForeignKey field.
126 - field - field, which contains object_id
127 - ct_field - field, which contains foreign key to content type
128 - processor - function, which should receive QuerySet of objects to be
129 loaded and return processed QuerySet. Used to set select_related etc.
130 """
