Changing fieldtype does nothing

Hi,

I’m developing an app using ERPNext v12.17

Right now I would like to change a Data field into a Select field for purposes of form entry. According to the documentation, I should use frm.set_df_property() to change the fieldtype (and options), and frappe should “refresh the field” as part of the function call.

Here’s what I tried:

frappe.ui.form.on('Test Doctype', {
    setup: (frm) => {
        frm.set_df_property('test_field', 'fieldtype', 'Select');
        frm.set_df_property('test_field', 'options', ['one', 'two']);
    },
    before_load: (frm) => {
        frm.set_df_property('test_field', 'fieldtype', 'Select');
        frm.set_df_property('test_field', 'options', ['one', 'two']);
    },
    onload: (frm) => {
        frm.set_df_property('test_field', 'fieldtype', 'Select');
        frm.set_df_property('test_field', 'options', ['one', 'two']);
    },
    refresh: (frm) => {
        frm.set_df_property('test_field', 'fieldtype', 'Select');
        frm.set_df_property('test_field', 'options', ['one', 'two']);
    },
    onload_post_render: (frm) => {
        frm.set_df_property('test_field', 'fieldtype', 'Select');
        frm.set_df_property('test_field', 'options', ['one', 'two']);
    },
});

After the code runs, the field remains a Data field, with no apparent change. Am I doing something wrong?

Looking through the frappe code base for instances of set_df_property, I don’t see any (apparent) lines of code to force a screen redraw, so I don’t understand why it doesn’t work for me.

NB: I’ve also tried running frm.refresh_field('test_field') after set_df_property, which also did not help.

1 Like

Hi @ebsjbulcher,
First of all you have to understand that frappe uses metadata to build its ui. The metadata saved in json format. Dynamically change the behaviour of the fieldtype is making your data inconsistent. So its better to change it directly in doctype of what kind of fieldtype you want to have for your field.

Hi @mrjurin, thanks for the reply!

frappe uses metadata to build its ui

I understand frappe uses metadata to build the UI; I’m trying to use the built-in API to alter the UI. I ran into numerous posts about people using set_df_property to change field types when I was researching my issue, and I’m trying to figure out why it doesn’t work for me?

Dynamically change the behaviour of the fieldtype is making your data inconsistent.

Why would changing the data type from Data to Select cause my data to be inconsistent? Both data types are stored as strings in the MariaDB backend.

So its better to change it directly in doctype of what kind of fieldtype you want to have for your field.

This doesn’t fit my use-case; allow me to explain

When entering in a new document, the field has a limited set of possible values. That set of values change every month. If I change the field type into a dropdown, then viewing old documents and hitting ‘save’ will overwrite the field with a value from the new set of options.

What I would like to do is offer my users a Select field if the document is a new form; otherwise, leave the field as a Data field, which is appropriate for the at-rest data. That will also make filtering in the list view work properly.

Hello
I have the same requirement as yours. So, did you find any solution?

I’m using a simple data field, so no.