What is best practice to create custom fields? fixtures or coding?

What is the good practice to create custom fileds?

  1. Fixtures
  2. Manual coding after_install - create_custom_fields()

From all tutorial, when create new custom fields, it is recommended to use export-fixtures. But when come to all good modules we saw, either from erpnext or else, nobody is using fixtures, i.e., erpnext_germany/erpnext_germany/install.py at version-15 · alyf-de/erpnext_germany · GitHub

After develop with ERPNext for a while, personally I also find fixtures error prone when working in team, and it is also hard to track changes as it always mess up.

The fixtures is it is always run on migration, sometime good sometime not desirable. I am not sure if there is way to prevent it form always updating.

But to create custom fields on after_install by coding, is also not very good as development proceeds IMHO. otherwise, as we have more custom fields, do we need to use patches? For me, not very good DX.

So, in this thread, I would like to find the best practice on how experiended developers is doing.

For me:

  • Modules, i.e., to be in marketplace - avoid using export-fixtures
  • Module for customer project - it is ok to use exort-fixtures

Note: In my first project, I was also using Export Customizations and that is totally horrible when come to team development.

What are your best practices?

2 Likes

For team of developers,

I am learning the hard way that…

  1. Export customizatoin form customize form shouldn’t be used at all (horrifying)
  2. bench export-fixtures also shouldn’t be used. It export too much stuff make fixture fields hard to maintain.
  3. Create the custom field, property setter by yourself (manually) and use function create_custom_fields() is the only way to ensure clean code.

I.e., hrms/hrms/setup.py at develop · frappe/hrms · GitHub

I would suggest you go with the third one if you don’t have many fields; otherwise, go with the first one. I have many custom fields, so I am using the first one because it is better compared to the second option.

why do you say the export customization form shouldnt be used can you elaborate

Because it will export any custom fields created for any apps altogether. So it made possible that it will update things (not done by you) unintentionally and super hard to keep track.

export-fuxture is the same thing but you can specify what to export.

But as I say create it by create_custom_fields() is best for full control but need some learning curve.

if i was using fixtures how can i add multiple property setters, like if i give
fixtures = [{
“doctype”: “Property Setter”,
filers = [ ‘doctype1’ ]
},
{
“doctype”: “Property Setter”,
filers = [ 'doctype2 ]
},
]
This one doesnt work for me even though this is the recommended way the doctype 2 overwirtes on doctype1, could you help me if you know how to add more that 1 property setter without overwritting

You can try this approach:

fixtures = [
    {
        "doctype": "Property Setter",
        "filters": [
            ["doc_type", "in", ["Doctype 1", "Doctype 2", "Doctype 3"]]
        ]
    }
]
1 Like