Monday, August 27, 2012

Current pages referenced for Django


https://docs.djangoproject.com/en/dev/ref/models/querysets/
https://docs.djangoproject.com/en/1.4/topics/db/queries/
https://docs.djangoproject.com/en/1.4/ref/models/relations/




Methods that return new QuerySets

Django provides a range of QuerySet refinement methods that modify either the types of results returned by the QuerySet or the way its SQL query is executed.

filter

filter(**kwargs)
Returns a new QuerySet containing objects that match the given lookup parameters.
The lookup parameters (**kwargs) should be in the format described in Field lookups below. Multiple parameters are joined via AND in the underlying SQL statement.

exclude

exclude(**kwargs)
Returns a new QuerySet containing objects that do not match the given lookup parameters.
The lookup parameters (**kwargs) should be in the format described in Field lookups below. Multiple parameters are joined via AND in the underlying SQL statement, and the whole thing is enclosed in a NOT().
This example excludes all entries whose pub_date is later than 2005-1-3 AND whose headline is "Hello":
Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello')
In SQL terms, that evaluates to:
SELECT ...
WHERE NOT (pub_date > '2005-1-3' AND headline = 'Hello')
This example excludes all entries whose pub_date is later than 2005-1-3 OR whose headline is "Hello":




https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.exists

exists

exists()
Returns True if the QuerySet contains any results, and False if not. This tries to perform the query in the simplest and fastest way possible, but it does execute nearly the same query. This means that calling QuerySet.exists() is faster thanbool(some_query_set), but not by a large degree. If some_query_set has not yet been evaluated, but you know that it will be at some point, then using some_query_set.exists() will do more overall work (one query for the existence check plus an extra one to later retrieve the results) than simply using bool(some_query_set), which retrieves the results and then checks if any were returned.

update

update(**kwargs)
Performs an SQL update query for the specified fields, and returns the number of rows affected.
For example, to turn comments off for all blog entries published in 2010, you could do this:
>>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False)
(This assumes your Entry model has fields pub_date and comments_on.)
You can update multiple fields — there's no limit on how many. For example, here we update the comments_on and headlinefields:










No comments:

Post a Comment