How to create Doctypes in ERPNext

In this blog, we will learn how to create new doctypes in ERPNext.
DocType is similar to a template in other frameworks (like Odoo).
When we create doctypes in ERPNext, a database table with the same name is created (with tab prefix)
for example – If doctype name is Library -> database table name will be tabLibrary
The directory structure for doctypes:-
library_management/library_management/doctype/__init__.py library_management/library_management/doctype/library/__init__.py library_management/library_management/doctype/library/library.js library_management/library_management/doctype/library/library.json library_management/library_management/doctype/library/library.py library_management/library_management/doctype/library/test_library.py
library.json: – JSON file that defines the attributes and fields of the document type (table columns)
library.js: – Client side controller for Form view
library.py: – Python controller (server side) for library document type
test_library.py: – Standard Python unit test model for writing tests
library.json
** (This file creates the doctype) **
{ "autoname": "format: PS-{####}", "doctype": "DocType", "fields": ( { "fieldname": "section1", "fieldtype": "Section Break", "label": "Section 1" }, { "fieldname": "field_1", "fieldtype": "Data", "in_list_view": 1, "label": "Field 1 Label", "reqd": 1 }, { "fieldname": "column_break1", "fieldtype": "Column Break", "label": "" }, { "fieldname": "field_2", "fieldtype": "Select", "in_list_view": 1, "label": "Field 2 Label", "options": "option1\option2", "reqd": 1 } ), "module": "library_management", "name": "Doctype Name", "owner": "Administrator", "permissions": ( { "amend": 0, "cancel": 0, "create": 1, "delete": 1, "email": 1, "export": 1, "if_owner": 0, "import": 0, "permlevel": 0, "print": 1, "read": 1, "report": 1, "role": "System Manager", "set_user_permissions": 0, "share": 1, "submit": 0, "write": 1 }, { "amend": 0, "cancel": 0, "create": 1, "delete": 0, "email": 0, "export": 0, "if_owner": 0, "import": 0, "permlevel": 0, "print": 0, "read": 1, "report": 0, "role": "All", "set_user_permissions": 0, "share": 0, "submit": 0, "write": 1 } ), "quick_entry": 1, "show_name_in_global_search": 1, "sort_field": "field1", "sort_order": "DESC", "title_field": "field1", "track_changes": 1, "track_seen": 1, "track_views": 1 }
library.py
**(This file contains business logic for a particular doctype)**
import frappe from frappe.model.document import Document class Library(Document): def before_save(self): # code to execute before saving a library record #
library.js
** (This file contains the js code for the form view) **
frappe.ui.form.on('Library', { refresh: function(frm) { // refresh method will run every time a form is refreshed // frm if the form object } });
Learn how to create a new application in ERPNext – Application creation and application installation process in ERPNext
Unique Doctypes in ERPNext
When a DocType has the Is Single option enabled, it becomes a Single DocType.
It does not create a new database table. All unique values stored in the tabSingles table. You can use it to store global settings.
We will use the `frappe.db.get_single_value(doctype_name, field_name)` method to obtain the value of a field from the doctype alone.
NEED HELP?
I hope you find the guide useful! Please feel free to share your thoughts in the comments below.
If you still have issues/questions regarding this, please create a ticket at https://webkul.uvdesk.com/en/customer/create-ticket/.
Please also explore our Odoo development services and a wide range of quality Odoo apps..
For any doubts, contact us at (email protected).
Thank you for your attention !!