Get specifics field in get_query function

Hello Everyone,

I’m trying to get 2 specific fields from my doctype when a use the link field get_query function.

cur_frm.fields_dict[‘distrito_no’].get_query = function(doc, cdt, cdn) {
return {
query: “app.app.doctype.doctype_name.doctype_name.get_distrito”,
filters:{ ‘codigo_regional’: doc.regional_no }
}
}

I have a server side function

def get_distrito(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql(""“SELECT codigo_distrito, nombre_distrito
FROM tabDistrito
WHERE codigo_regional = {0} “””.format(filters.get(“codigo_regional”)))

the result is exactly what i want but when i select the value, the field is cleaned by itself.

What am i doing wrong?

Help!!

also set as_dict=1 in your frappe.db.sql method.

Do you have an error trace in your js console?

I just figured this out @geekroot. This is what happened to me and I have a similar issue.

You need to select the name column of your custom DocType as the first column. When selecting the item in the dropdown it’s “cleaning itself” because a validation function is run to validate the selected item. It validates the item by it’s first selected column which is expected to be the name aka the primary key of your table.

So your query should look like:
select name, codigo_distrito, nombre_distrito from...

@rmehta:

This is the relevant function. Is there a way to manipulate the value that is sent to this function to have other values but the name as the first column?

Or maybe there’s already an inbuilt solution for the validate_link function on server-side so it doesn’t assume that the value it is provided is the name of the doc but some other column?