Yeah, I think you could get it to work. For the sake of an example lets says its âqty * price = totalâ
I would make a custom field called total_amount thatâs a read_only type, which wonât create a column in the DB. On the doctype you want to compute it with a property:
@property
def total_amount(self):
return self.qty * self.price
# with default value of zero
# return self.qty * self.price if self.qty and self.price else 0
In JS youâd want to do something similar to actually compute it. The @property on the python side wonât translate to the UI automatically.
Form should be handled more like the case above. In the form view, the document goes through Frappeâs frappe.get_doc load abstraction so you have a simpler path.
In the list view, it uses the frappe.get_list method, which just returns the results of the fields. You might be able to achieve this (I havenât tried) by adding your âvirtual fieldâ to have âin list viewâ and then using a formatter to do the computation. You probably have to add the fields youâre basing the computation on to the add_fields key. Docs: List
The idiomatic approach here is to calculate the value and store it. Just because you can compute these things on the fly doesnât mean you should. Conversely, the portal API has a utility to provide your own listview function but thatâs not part of the design in the app/ERPNext.
Thanks for trying to help me out, but Iâm just starting with frappe and your explanations are too advanced for poor little me.
What I tried so far:
I created a DocType called âVirtual Fieldâ with these 3 fields: Article(Data), Price(Int), Amount(Int).
I would like to create a List View that shows the Columns Article, Price, Amount and Total.
As expected Total will be computed Price * Amount.
I also created a ClientScript:
frappe.listview_settings['Virtual Field'] = {
hide_name_column: true,
add_fields: ['total'], // Q: How should frappe know the column label of this field?
formatters: {
total(val) {
return "See me!"; // Q: I get only passed the value of one field, how do I access the other fields?
}
}
}
This of course doesnât work, it just shows my skill level and what I gleaned from your answer. It would be great if you could help me to work this out.
Did you manage to figure this out. Iâm trying to do similar thing by having a calculated total row (qty * price) or for example taxes (total * taxe_rate) in a list view.