Wednesday, July 6, 2016

Get Doctype Field Value Using Python

get_value (self, doctype, filters=None, fieldname=name, ignore=None, as_dict=False, debug=False, cache=False)
Returns a document property or list of properties.
Parameters:
  • doctype - DocType name.
  • filters - Filters like {"x":"y"} or name of the document. None if Single DocType.
  • fieldname - Column name.
  • ignore - Don't raise exception if table, column is missing.
  • as_dict - Return values as dict.
  • debug - Print query in error log.
Example:
# return first customer starting with a
frappe.db.get_value("Customer", {"name": ("like a%")})

# return last login of **User** `test@example.com`
frappe.db.get_value("User", "test@example.com", "last_login")

last_login, last_ip = frappe.db.get_value("User", "test@example.com",
    ["last_login", "last_ip"])

# returns default date_format
frappe.db.get_value("System Settings", None, "date_format")

4 comments:

  1. where should i write these codes?
    I mean in which file?

    ReplyDelete
  2. Hi, do you know how to do this with child tables? I've been trying in several ways but it seems as I need special permissions to acces them

    ReplyDelete
  3. This is my code:

    proitems = conn.get_list('Sales Invoice Item', filters={'docstatus': ('=', 1)}, fields=['item_name'])



    response:

    FrappeException Traceback (most recent call last)
    in
    1 proitems = conn.get_list('Sales Invoice Item',
    2 filters={'docstatus': ('=', 1)},
    ----> 3 fields=['item_name'])
    4 proitems

    c:\users\jorge\frappe-client\frappeclient\frappeclient.py in get_list(self, doctype, fields, filters, limit_start, limit_page_length, order_by)
    86 res = self.session.get(self.url + "/api/resource/" + doctype, params=params,
    87 verify=self.verify, headers=self.headers)
    ---> 88 return self.post_process(res)
    89
    90 def insert(self, doc):

    c:\users\jorge\frappe-client\frappeclient\frappeclient.py in post_process(self, response)
    280
    281 if rjson and ('exc' in rjson) and rjson['exc']:
    --> 282 raise FrappeException(rjson['exc'])
    283 if 'message' in rjson:
    284 return rjson['message']

    FrappeException: ["Traceback (most recent call last):\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/app.py\", line 62, in application\n response = frappe.api.handle()\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/api.py\", line 119, in handle\n doctype, **frappe.local.form_dict)})\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py\", line 1055, in call\n return fn(*args, **newargs)\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/client.py\", line 32, in get_list\n check_parent_permission(parent, doctype)\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/client.py\", line 378, in check_parent_permission\n raise frappe.PermissionError\nPermissionError\n"]

    ReplyDelete