Naming
All DocTypes in Frappe have a primary key called name
. This is the unique id by
which you will be finding records and manipulating them using the ORM.
You can configure how docs
should be named when a new document is created.
The following are the ways you can setup naming in a DocType.
DocType autoname
You can set the name by the autoname
property of the DocType.
1. field:[fieldname]
The doc
name is fetched from the value of the field provided.
2. [series]
You can provide a naming pattern which will be incremented automatically. For e.g, if you set it as PRE.#####
, the first document created will have the name
as PRE00001, and second one will be PRE00002 and so on.
3. naming_series:
The naming pattern is derived from a field in the document. For e.g, you have a field naming_series
in your document and it's value is set as PRE.#####
, then that will be the pattern used for generating the name. This value can change per document. So the next document can have a different pattern.
This works only if you have a field called
naming_series
in your DocType.
4. Prompt
If you set it as Prompt, the name is required to be filled in manually.
5. Format
This is the most flexible one when it comes to configuring your naming schemes.
Let's say we have
EXAMPLE-{MM}-test-{fieldname1}-{fieldname2}-{#####}
Everything outside the curly braces are plain text. Keywords inside the curly braces will be evaluated based on what they represent. In this case:
- MM: will be replaced by the current month
- fieldname1: will be replaced by the value of
fieldname1
in the document - #####: will generate a series, which starts with
00001
So the final name may look like, EXAMPLE-02-test-value1-value2-00001
By Controller Method
You can also define a name programatically by declaring an autoname
method in the controller class. Example
from frappe.model.naming import getseries
class Project(Document):
def autoname(self):
# select a project name based on customer
prefix = `P-{}-`.format(self.customer)
self.name = getseries(prefix, 3)
By Document Naming Rule
You can also create rules for naming DocTypes by creating Document Naming Rule
You can create multiple Document Naming Rules for a particular doctype that can be applied selectively based on filters.
To define a Document Naming Rule you have to specify
- Document Type it is being applied on
- Priority of the rule (rules with higher priority will be applied first)
- Conditions to apply the rule
- Naming Rules
Numbering
You can define various numbering prefixes for the rule based on the conditions defined. This is done by setting a prefix and the number of digits for that rule.
For example if you are creating a separate numbering for high priority todos:
- Prefix: todo-high-
- Digits: 3
Will lead to numbering like todo-high-001
, todo-high-002
and so on.
Priority of Naming
Naming priority is as follows
- Document Naming Rule
autoname
controller method.autoname
DocType property
Special Rules
- Child DocTypes do not follow naming rules
- Amended documents have a suffix (
-1
,-2
etc) to the original document