Version 11 (modified by 17 years ago) ( diff ) | ,
---|
The queryset-refactor branch
(Up to date as of 22 March, 2008)
This branch contains a major refactoring of the django.db.models.query.QuerySet
class to fix a group of SQL problems and make SQL generation easier for database backends requiring customization.
How to get the branch
svn co http://code.djangoproject.com/svn/django/branches/queryset-refactor
See our branch policy for full information on how to use a branch.
Status
Branch was created on 13 September, 2007.
At the moment, the branch should be fairly usable for people wishing to test (with the exception of the Oracle backend, as noted below). It is not appropriate for production usage. Some of the new features, such as model inheritance still have rough edges or are incomplete, but all the porting work is complete and code that fixed existing bugs should all work. There are still some internal changes that need to be made prior to merging with trunk, so any code that tries to rely on internal methods may need to change before the next release.
Bug reports against code not listed in the work in progress section should be filed in Trac. Please take some extra effort to check for duplicates first.
Tickets of interest for this branch are marked with qs-rf (and qs-rf-fixed when fixed) in the keywords field in Trac. This report shows all the tickets being worked on (To view qs-rf with the qs-rf-fixed tagged tickets, view this report).
New features
Along with, and as part of, all the bug fixes mentioned above there are a number of new features in the branch. A number of these features are purely internal details, but there are a few that add extra public functionality.
- Ordering querysets across related models has a new syntax that is the same as the way you specify relations in a filter:
field1__field2__field3
, etc. The new syntax is more natural and consistent, as well as helping solve a few bugs. See theorder_by()
documentation in db-api.txt for more information and some examples. - Model inheritance is now possible. Both abstract base classes and multi-table inheritance are possible. See the model-api.txt documentation for details.
- The
__iter__()
method on querysets does not pull all the results into memory immediately. This reduces memory usage for large querysets where you don't end up accessing all the results. Queryset caching still occurs, though, so a single queryset object will only hit the database once. This change means that testing the boolean value of a queryset will only pull in the first few rows of the result, not all of them. - Slicing a queryset from a particular value to the end of a queryset is possible.
- Querysets have a
reverse()
method that reverses whatever the current ordering is. - The queryset
values()
method can retrieve fields that are related via aForeignKey
orOneToOneField
relation. - A new
valueslist()
method has been added to querysets. This is similar tovalues()
except that it returns a list of tuples, rather than a list of dictionaries. - You can specify a list of related fields to traverse in a
select_related()
call. This provides a way to select only the related data you are interested in. Only single-valued relations can be selected in this way, however (notManyToManyFields
). - Filtering a queryset by checking if a field attribute is
None
is equivalent to testing if the corresponding database column isNULL
. Soqs.filter(foo=None)
is now identical toqs.filter(foo__isnull=True)
. - An
update()
method has been added to querysets to allow multiple objects to have an attribute updated in a single SQL query. Q
classes now fully support&
,|
and~
to combine them in pairs as conjunctions or disjunctions or to negate the sense of a filter, respectively (&
and|
were previously supported, but returned a different type of class). Thus theQAnd
,QOr
andQNot
classes are no longer required and have been deprecated. Using them raises a warning (although they still work as before).
Backwards incompatible changes
A few backwards incompatible changes are created by the changes in this branch. Most people won't be affected by many of these, and porting code is reasonably straightforward.
- The
Options.get_order_sql()
method is now gone indjango.db.models.options
. There appears to be no use for this method any longer. Q
objects have changed internally. This is only relevant if you have created custom Q-like objects. You would have created aget_sql()
method that returned a data structure that was inserted into the query. In the new code, you create aadd_to_query()
method that accepts one argument -- thedjango.db.models.sql.query.Query
instance for the current query. Your Q-like object can then add to the various attributes of this class (select
,where
, etc) to have whatever effect it likes on the result. Note that theadd_to_query()
method is called when the object is added to theQuery
object and more changes may be made before it is turned into SQL and executed against the database.- The
OneToOneField
class has finally been updated, as the documentation has indicated would be happening for a long while. There are few externally visible changes, with one exception: aOneToOneField
is no longer automatically the primary key for a model that includes it. It still accepts theprimary_key
attribute, however, so you should addprimary_key=True
to the declaration of any existingOneToOneField
instances in your code to preserve backwards compatibility. - If you pass a bad field name into a filter or
order_by()
, Django now raisesFieldError
(fromdjango.core.exceptions
), rather than Python's built inTypeError
. Also, the list of legal field names is now sorted alphabetically for easier searching. This should have no effect on most production code, however some test suites may need to be updated to accommodate the changed traceback output. - It is possible to use extra select fields -- those included via
extra(select=...)
-- for ordering the results. Previously, those fields could be specified to theorder_by()
method. Due to increased error checking, that is no longer practical. Instead, pass those extra columns to theorder_by
argument of theextra()
method:qs.extra(select={'a': ...}).order_by('a') # Old style qs.extra(select={'a': ...}, order_by=('a',)) # New style
- Still on
extra(select=...)
... if you want to substitute parameters into these extra selection columns, use theselect_params
argument toextra()
. Theparams
argument is only applied to the extra where conditions. select_related(False)
is no longer possible. Don't worry. You didn't know this existed, so you won't miss it. It was never part of the official API.- There is a slight difference between these two filter statements
This difference only applies when
qs.objects.filter(f1).filter(f2) qs.objects.filter(f1, f2)
f1
andf2
are referencing the same multi-valued relationship (aManyToManyField
or reverseForeignKey
). The first version allows filtering against different items from the relationship (things that matchf1
on one object in the related table as well asf2
on another object in the related table), whereas the second version's filters will be applied to the same item. See the database API documentation section called "Lookups that span relationships" for details.
Work in progress
If you are testing this branch, there are a few areas that need caution. These are known places that are work-in-progress, so expecting them to work perfectly (or even at all) would be optimistic. Obviously, in addition to these points there are a lot of little things being tweaked and fixed, as well as optimisation work going on. That's to be expected on a branch.
- Model inheritance has not been integrated into the admin. It's unclear at this time whether it will be worth doing this for existing admin or just port straight to newforms-admin. In any case, trying to use multi-table inheritance via the admin interface won't work.
OneToOneField
in the admin interface has similar problems.