Extend / inherit existing Doctype from a custom app

Hi,

Is there a way to extend an existing Doctype, but store the modification in an app? The most common solution is usually creating an inheritance between the new Doctype and an existing one.

Sample usage: a company sell wines, we could extend the “Item” Doctype to store dedicated data to the wine products (bottle size, package, region, alcohol level, …)

If it’s not possible to have a inherited new Doctype, is there a way to customize a Form / Fields, but store the changes in the custom app (and not in the original app “Stock”)? I mean the new custom fields in the Item Doctype would be managed and setup by installing the custom app.

I won’t start with extending/inhereting the doctype first. It’s a nightmare to maintain overridden code.

  1. If you need extra fields just add custom fields - https://docs.erpnext.com/docs/user/manual/en/customize-form and https://docs.erpnext.com/docs/user/manual/en/custom-field
  2. If you need some business logic on those extra fields:
    1. Use server scripts - Server Script OR
    2. Use doc_event hooks from app - Hooks
  3. If you STILL can’t achieve all the functionality then override the entire class - Hooks

In general, “prefer extending over overriding” and your customizations will be maintainable long term.

I totally agree.

Actually I was more thinking by “inheriting” => just keeping all the current base Doctype logic the same, but managing the code for the custom fields and logic inside the custom app.

For example in Odoo this can be done by creating a new “Model” (equivalent to Doctype), inherited from “Product” (equivalent to “Item”), and then this allows to extend the Product model by adding custom fields from the inherited model. This actually does not create a new SQL table, but adds custom fields in the source one.

When I add a custom field, is there a way to “attach” it to a custom app? What I’m looking for: the installation of the custom app should automatically add the custom fields to existing ERPnext Doctypes?

How to handle that?

1 Like

And more globally my question is also for scripts, or any other customization.

Let say I want to create a custom client script for the Item Doctype (which is a standard ERPnext Doctype from “Stock” module) to add buttons, or do other actions / display customization in Item form and list.

Is there a way to have this client script saved in my custom app to be easily maintained / deployed / updated with the custom app?

Yes. It works exactly like you mentioned.
You create a custom app and place your scripts/customisations in that app.

This should be helpful. Create an App (frappeframework.com)

Yes I’ve seen this very instructive custom app creation demo, it’s really nice to have a better understanding on how it works.

But this app does not use already existing Doctypes, but only new ones.

Are you sure I could store a script concerning one existing Doctype (let say Item) in the custom app? If yes what is the correct way to proceed?

@fred92 , this is an excellent question.

I am currently in a similar situation and would like to learn more.

I find it very hard to create a holistic development lifecycle around changes in ERPNext.

We only want to create some new DocTypes and use existing ones, but we haven’t found any way to extract those changes after development, manage them with git and introduce them into a ci pipeline.

We are now following a similar approach because we believe custom apps might the feature we have to use. However, we still want to use default ERPNext DocTypes as well.

What if I want to create new Item Groups, or new Item Templates and Variants? They will all live in the Stock module. But that’s not part of my custom app, it is part of the ERPNext app. How could I get those chances out of there? And sadly, this brings us back to square one. :confused:

I’ve never personally done this. But my understanding is that your requirement can be handled by Fixtures.
https://frappeframework.com/docs/user/en/guides/app-development/how-to-create-custom-fields-during-app-installation

1 Like

You have the option to export customizations (custom fields) to a module (your custom app) which would add all the custom fields to the necessary doctypes when the custom app is installed (this is how hrms works).
This can be done in the customize form page under actions button.

As for scripts, if you write them as APIs in a python file within the custom app and call them via hooks, then you don’t need any fixtures.
But if you’re using client scripts and server scripts in the desk, then you need to add those in hooks as fixtures, and give “bench export-fixtures” in order to add them while installing the script. (Fixtures can be used for any other records in almost any other doctypes.)

Currently I believe fixtures is the only way to get all your workflows, notifications, role permissions, roles, property setters (modifications to the fields of standard doctypes) into new sites on installing a custom app.