Jump to: navigation, search

Database Query API

175 bytes added, 22:33, 9 February 2016
no edit summary
Each of these QuerySets allows you to chain together a series of QuerySet method calls. QuerySet supports the following methods:
* .proxyselect(name[fields...]) - returns generator* .order(fieldname, args...) - applies a namedorder-proxy (by fields given as strings; use "living-name", "private", or "referenced")for descending
* .filter(obj, args...) - applies a FilterObject or Python callable
* .map(f) - applies a function to all selected objects
* .proxy(name, args...) - applies a named-proxy ("living", "private", or "referenced")
* .limit() - set start or limit or selection
* .count() - returns the number of matches
* .limit() - set start or limit or selection
* .order(fieldname, ...) - order-by fields given as strings; use "-name" for descending
* .select([fields...]) - returns generator
* .tag(tag_name) - puts a tag on all selected items, if not tagged with tag_name already
These methods can be lined up to create a series of operations:
db.Person.filter(lambda person: person.private=True).select("gramps_id")
== ==
As an example, consider selecting the gramps_id from all people who have a surname of "Smith" and whose name begins with a "J", ordered by the gramps_id:
== Database db.QuerySetPerson.filterselect("gramps_id", "handle") ==
 == Database.QuerySet.filterorder() ==
Barry, I0011
== Database.QuerySet.filter() == == == == Database.QuerySet.proxy() == == Database.QuerySet.limit() == == Database.QuerySet.count() == == Database.QuerySet.tag() == == Joins ===
The method can also do joins across primary objects in each of the WHERE, ORDER, or select fields. For example consider this request:
Note that these joins are done per record and are therefore not yet optimized---each requires another database access. In the future, these could be optimized via a SQL JOIN.
== Implementation ==

Navigation menu