Executing Code On Doctype Events
To execute code when a DocType is inserted, validated (before saving), updated, submitted, cancelled, deleted, you must write in the DocType's controller module.
1. Controller Module
The controller module exists in the
doctype
folder in the Module of the
DocType
. For example, the controller for
ToDo
exists in
frappe/desk/doctype/todo/todo.py
(version 5). A controller template is created when the DocType is created. which looks like
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
class CustomType(Document):
pass
2. Document Properties
All the fields and child tables are available to the class as attributes. For example the
name
property is
self.name
3. Adding Methods
In this module, you can add standard methods to the class that are called when a document of that type is created. Standard Handlers are:
-
autoname
: Called while naming. You can set theself.name
property in the method. -
before_insert
: Called before a document is inserted. -
validate
: Called before document is saved. You can throw an exception if you don't want the document to be saved -
on_update
: Called after the document is inserted or updated in the database. -
on_submit
: Called after submission. -
on_cancel
: Called after cancellation. -
on_trash
: Called after document is deleted.