How To Make Server-Side Methods (And Use frappe.get_all() )


I currently have a web page that displays a table of customers and their respective orders, and what I want to do is write a function that simply calculates the sum of the prices of all items in an order.

Where do I write such a function? The web page was created in the www folder along with its respective Python file.

Also, running frappe.get_all() in get_context() to fetch all DocTypes of one type is not yielding anything.

So, instead of using the following:

def get_orders():
    return frappe.get_all("Order",
                          fields=["first_name", "last_name", "address", "order"],
                          order_by="last_name desc")

I have had to crudely rely on creating a list of objects like so:

def get_context(context):
    context.orders = [frappe.get_doc("Order", "2995f8477d"),
                      frappe.get_doc("Order", "170fe55ed2"),
                      frappe.get_doc("Order", "98eb82155c"),
                      frappe.get_doc("Order", "39e10adb32"),
                      frappe.get_doc("Order", "a0ecfce3de")

How do I get this function to work and how do I incorporate the function for finding the sum of items in a list?


Have you tried, here is the link

1 Like

So, what do I do with this, exactly? How does it work?

Do I write a JavaScript file in addition to the HTML and Python files?

Can retrieve data from all documents of a certain type?

I need a detailed explanation here because this doesn’t seem like the right solution to my problem. I’m trying to execute a function in Jinja.

def get_orders():
	orders = frappe.get_all('Order')
	for d in orders:
		p = frappe.get_doc('Order', d)
		# for first_name -> p.first_name
		# p.last_name
		# p.order

If order field had multiple entries or maybe was a child table of ‘Order’ then maybe you can iterate

for order in p.order:
	sum += order.sum

I hope this gives you some ideas to work on


I already tried entering my own functions in but have been unable to call them in orders.html via Jinja. Plus, frappe.get_all just ain’t working for some reason…

Try this, in your terminal type “bench console” and hit enter. It should open up a bench console. In that type “frappe.get_all(‘Order’)” , this should fetch and return all the Order

OK, so it did return a list of dictionaries in the terminal. Does the value in each key represent the DocType object?

Oddly, Jinja is still treating the “orders” variable I made as an empty list. In the Python file, I simply did:

def get_context(context):
    orders = frappe.get_all("Order")

Any solutions to this. I am experiencing the same thing.
When calling the frappe.get_all() method in python it has troubles fetching child tables.
If I specify certain fields with the field parameter like frappe.get_all('Project', filters={'status': 'Open'}, fields=['name', 'description']) it returns data. As soon as I add a child table field to the field list the complete result is empty.

Try this maybe

In [2]: items_with_reviews = frappe.get_all("Hub Item", fields="`tabHub Item`.name, `tabHub Item`.hub_category, `tabHub Item Review`.content")

Found it here

1 Like