Error when save a new record in a DocType

Hello,

I create a new DocType, and in one link field, I need to show Description instead Name

So, I create these:

@frappe.whitelist()
def get_cidade_name(id_cidade=None):
    if id_cidade:
        return frappe.db.sql(
            """select tabCidade.descricao from tabCidade where tabCidade.name="%(name)s" """ % {'name': id_cidade})

and this:

frappe.ui.form.on("Contato", "cidade", function (frm, cdt, cdn) {
    var d = frappe.get_doc(cdt, cdn);

    return frm.call({
        method: "intranet.controllers.queries.get_cidade_name",
        child: d,
        args: {
            "id_cidade": d.cidade
        },
        callback: function (r) {
            frappe.model.set_value(cdt, cdn, "cidade", r.message);
        }
    });
});

Works fine, show me description correctly, but… when I try to save the DocType, these error show to me:

Traceback (innermost last):
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/app.py", line 69, in application
    response = frappe.handler.handle()
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/handler.py", line 20, in handle
    execute_cmd(cmd)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/handler.py", line 37, in execute_cmd
    ret = frappe.call(method, **frappe.form_dict)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/__init__.py", line 798, in call
    return fn(*args, **newargs)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/desk/form/save.py", line 12, in savedocs
    doc = frappe.get_doc(json.loads(frappe.form_dict.doc))
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/__init__.py", line 530, in get_doc
    return frappe.model.document.get_doc(arg1, arg2)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/document.py", line 46, in get_doc
    return controller(arg1, arg2)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/document.py", line 82, in __init__
    super(Document, self).__init__(arg1)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 47, in __init__
    self.update(d)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 70, in update
    self.set(key, value)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 117, in set
    self.extend(key, value)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 143, in extend
    self.append(key, v)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 138, in append
    raise ValueError, "Document attached to child table must be a dict or BaseDocument, not " + str(type(value))[1:-1]
 ValueError: Document attached to child table must be a dict or BaseDocument, not type 'list'

Any ideia why?

Regards.

You don’t need to pass “child” argument in frappe.call. Passing child does additional post-processing that updates the child record automatically and that might be causing issues if the server side method returns something unexpected

@anand

I change my function to these:

frappe.ui.form.on("Contato", "cidade", function (frm, cdt, cdn) {
    var d = frappe.get_doc(cdt, cdn);

    return frm.call({
        method: "intranet.controllers.queries.get_cidade_name",
        args: {
            "id_cidade": d.cidade
        },
        callback: function (r) {
            frappe.model.set_value(cdt, cdn, "cidade", r.message);
        }
    });
});

and same problem…

Traceback (innermost last):
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/desk/form/save.py", line 12, in savedocs
    doc = frappe.get_doc(json.loads(frappe.form_dict.doc))
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/__init__.py", line 530, in get_doc
    return frappe.model.document.get_doc(arg1, arg2)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/document.py", line 46, in get_doc
    return controller(arg1, arg2)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/document.py", line 82, in __init__
    super(Document, self).__init__(arg1)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 47, in __init__
    self.update(d)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 70, in update
    self.set(key, value)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 117, in set
    self.extend(key, value)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 143, in extend
    self.append(key, v)
  File "/Users/fellipeh/Developer/frappe/apps/frappe/frappe/model/base_document.py", line 138, in append
    raise ValueError, "Document attached to child table must be a dict or BaseDocument, not " + str(type(value))[1:-1]
 ValueError: Document attached to child table must be a dict or BaseDocument, not type 'list'

Any ideia?

How come you are calling form save?

Hello @rmehta,

Just click on Save Button… I don’t have any code in my “DocType” python file…

I`m using only 2 files:

This, to get the correctly field:

@frappe.whitelist()
def get_cidade_name(id_cidade=None):
    if id_cidade:
        return frappe.db.sql(
            """select tabCidade.descricao from tabCidade where tabCidade.name="%(name)s" """ % {'name': id_cidade})

and this JS, to try modify the “action” :

frappe.ui.form.on("Contato", "cidade", function (frm, cdt, cdn) {
    var d = frappe.get_doc(cdt, cdn);

    return frm.call({
        method: "intranet.controllers.queries.get_cidade_name",
        args: {
            "id_cidade": d.cidade
        },
        callback: function (r) {
            frappe.model.set_value(cdt, cdn, "cidade", r.message);
        }
    });
});

I think the problem is how frappe works after I change the value in field… frappe try to get the correctly FK record, but, doesn’t find out.

Any news about this?

@fellipeh what is the problem?

MariaDB does that. Check your query.