Models are responsible for persisting data and containing application logic.
Models are named with singular nouns (for example: Page, User, Image, Bear, etc.). Breaking convention is okay, but scaffolding will not work properly without some help.
Each model class should be in it’s own file under /app/models and the name of the file should be the underscored class name. For example, to create a model to represent furry bears, you would create the file /app/models/furry_bear.py and inside of that define a class named FurryBear.
The Model class is built on top of App Engine’s google.appengine.ext.ndb module and there is nothing stopping you from directly using any regular ndb.Model class with Ferris. Documentation on propeties, querying, etc. can be found here.
Base class that augments ndb Models by adding easier find methods and callbacks.
Generates an ndb.Query with filters generated from the keyword arguments.
Example:
User.find_all_by_properties(first_name='Jon',role='Admin')
is the same as:
User.query().filter(User.first_name == 'Jon', User.role == 'Admin')
Similar to find_all_by_properties, but returns either None or a single ndb.Model instance.
Example:
User.find_by_properties(first_name='Jon',role='Admin')
The Model class automatically generates a find_by_[property] and a find_all_by_[property] classmethod for each property in your model.These are shortcuts to the above methods.
For example:
Show.find_all_by_title("The End of Time")
Show.find_all_by_author("Russell T Davies")
Additionally the Model class provides aliases for the callback methods. You can override these methods in your Model and they will automatically be called after their respective action.
Called before an item is saved.
Parameters: | self – refers to the item that is about to be saved |
---|---|
Note : | self.key is invalid if the current item has never been saved |
Called after an item has been saved.
Parameters: |
|
---|
Called after an item is retrieved.
Parameters: | key – Is the key of the item that is to be retrieved. |
---|
Called after an item has been retrieved.
Parameters: |
|
---|
Called before an item is deleted.
Parameters: | key – is the key of the item that is about to be deleted. It is okay to get() this key to interogate the properties of the item. |
---|
Called after an item is deleted.
Parameters: | key – is the key of the item that was deleted. It is not possible to call get() on this key. |
---|
These methods are useful for replicating database triggers, enforcing application logic, validation, search indexing, and more.
The BasicModel adds automatic access fields:
Adds the common properties created, created_by, modified, and modified_by to Model
Stores the created time of an item as a datetime (UTC)
Stores the modified time of an item as a datetime (UTC)
Stores the user (a google.appengine.api.users.User) who created an item.
Stores the user (a google.appengine.api.users.User) who modified an item.