For example, to retrieve all the entries where the author’s name is the same as the blog name, we could issue the query: statement, the percent sign signifies a multiple-character wildcard and the underscore signifies a single-character wildcard.) This means things should work intuitively, so the abstraction doesn’t leak.
For example, to retrieve all the entries that contain a percent sign, just use the percent sign as any other character: method is applied instantly and returns the number of rows matched by the query (which may not be equal to the number of rows updated if some rows already have the new value).
The only restriction on the to update one field based on the value of another field in the model.
For example, to increment the pingback count for every entry in the blog: .
(Behind the scenes, this functionality is implemented by Python descriptors.
This shouldn’t really matter to you, but we point it out here for the curious.) Django also creates API accessors for the “other” side of the relationship – the link from the related model to the model that defines the relationship.
To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table.
To create an object, instantiate it using keyword arguments to the model class, then call s for you automatically, behind the scenes.
To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.
This example retrieves all If you are filtering across multiple relationships and one of the intermediate models doesn’t have a value that meets the filter condition, Django will treat it as if there is an empty (all values are In the examples given so far, we have constructed filters that compare the value of a model field with a constant.
But what if you want to compare the value of a model field with another field on the same model?
Django provides act as a reference to a model field within a query.
These references can then be used in query filters to compare the values of two different fields on the same model instance.
For example, to find a list of all blog entries that have had more comments than pingbacks, we construct an object with a double underscore will introduce any joins needed to access the related object.