Programmatically create a DocType

Hi,

I’m trying to create a doctype from my custom app. I wanted to set ‘Prompt name’ to enter. My code as bellow.

{
“actions”: [],
“allow_import”: 1,
“autoname”: “Prompt”,
“creation”: “2023-12-05 16:34:01”,
“description”: “Adds a custom e a DocType”,
“doctype”: “DocType”,
“document_type”:“Document”,
“engine”: “InnoDB”,
“field_order”: [
“name”,
“value”
],
“fields”: [
{
“fieldname”: “name”,
“fieldtype”: “Int”,
“in_list_view”: 1,
“in_standard_filter”: 1,
“label”: “Name”,
“reqd”: 1
},
{
“fieldname”: “value”,
“fieldtype”: “Int”,
“in_list_view”: 1,
“in_standard_filter”: 1,
“label”: “Value”,
“reqd”: 1
}
],
“links”: [],
“modified”: “2023-12-05 16:34:01.717985”,
“modified_by”: “Administrator”,
“module”: “Custom module”,
“name”: “Custom doctype”,
“naming_rule”: “Set by user”,
“owner”: “Administrator”,
“permissions”: [
{
“create”: 1,
“delete”: 1,
“email”: 1,
“print”: 1,
“read”: 1,
“report”: 1,
“role”: “System Manager”,
“share”: 1,
“write”: 1
},
{
“create”: 1,
“delete”: 1,
“email”: 1,
“print”: 1,
“read”: 1,
“report”: 1,
“role”: “Administrator”,
“share”: 1,
“write”: 1
}
],
“sort_field”: “modified”,
“sort_order”: “ASC”,
“states”: [],
“track_changes”: 1
}

this naming rule and auto name is showing when i search doctype list and go inside Custom doctype. But when i go the customize form it’s blank. I assume this is the reason I can’t see the name field when i try to create an item in this custom doctype.

Could you please help me with this?

Thank you.

i have one method but it give me an error

@frappe.whitelist(allow_guest=True,methods=[“POST”])
def create_doctype(doctype_name, fields):
try:
frappe.get_doc(“DocType”, doctype_name)
print(“@@@@@@@@@@@@@@@@@@@@@@@@@@@@Doctype already exists:”, doctype_name)
except frappe.DoesNotExistError:
print(“@@@@@@@@@@@@@@@@@@@@@@@@@@@@:”, doctype_name)
doctype = frappe.new_doc(“DocType”)
print(“@@@@@@@@@@@@@@@@!!!@@@@@@@@@@@@@:”, doctype)

  doctype.update({
     "doctype_name": doctype_name,
     "fields": fields,
     "module": "Custom"
  })
  print("@@@@@@@@@@@@@@@@!!!@#@#@#!!!!!!@@@@@@@@@@@@@:", doctype)
  
  doctype.insert()
 

  print("Doctype created:", doctype_name)

create_doctype( “dec5_doctype118”,
[
{“fieldname”: “field_1”, “fieldtype”: “Data”, “label”: “Field 1”},
{“fieldname”: “field_2”, “fieldtype”: “Int”, “label”: “Field 2”},
{“fieldname”: “field_3”, “fieldtype”: “Date”, “label”: “Field 3”}
])

did you import Frappe?

yes using import frappe method .

Hey Everyone!
I was able to accomplish this using the REST API, i created a simple python example that can be a good starting point.

The most important parameter that you cannot miss is "custom": 1 otherwise it will break Frappe and you need to restore database.
See the gist below: