Frappe Get List: How to Fetch Records
The short answer: in Frappe/ERPNext, you fetch a list of records with frappe.get_list(doctype, filters, fields), which returns an array of dictionaries—or just names if you pass fields=["name"]. This is the standard, built-in way to query the database and is used in server-side Python scripts, custom apps, and API endpoints.
What Is frappe.get_list?
frappe.get_list is a method in the Frappe framework (the open-source web framework that powers ERPNext) that retrieves records from a DocType. It is the lightweight counterpart to frappe.get_all—in fact, get_list is an alias for get_all in most contexts. Both run a SELECT query against the database and return a list of dictionaries, each representing a document's field values.
Unlike frappe.get_doc, which loads a full document object (with all child tables, permissions, and workflow state), get_list only fetches the fields you specify. This makes it much faster and more memory-efficient when you only need a few columns from many records.
Basic Syntax and Parameters
The method signature is:
frappe.get_list(doctype, filters=None, fields=None, order_by=None, limit_page_length=20, start=0, as_list=False, ...)
Key parameters you'll use daily:
- doctype (required): The name of the DocType, e.g., "Customer", "Sales Order", or a custom DocType.
- filters: A dict or list of conditions, e.g.,
{"status": "Open"}or[["age", ">", 30]]. - fields: A list of fieldnames to return, e.g.,
["name", "customer_name", "status"]. If omitted, it defaults to["name"]. - order_by: A string like
"creation desc"to sort results. - limit_page_length: Number of records to return. Set to
0to get all (but be cautious with large tables). - as_list: If
True, returns a list of tuples instead of dicts.
Example: get the names and statuses of all open sales orders, sorted by creation date, newest first:
orders = frappe.get_list("Sales Order", filters={"status": "Open"}, fields=["name", "status"], order_by="creation desc")
How to Use Filters Effectively
Filters are where most of the power lies. You can pass a simple dict for equality conditions, or a list of tuples for more complex comparisons. Here are common patterns:
- Equality:
{"customer": "Acme Corp"} - Comparison:
[["grand_total", ">", 1000]] - Multiple conditions (AND):
{"status": "Open", "customer": "Acme"}or[["status", "=", "Open"], ["grand_total", ">", 1000]] - OR logic:
[["status", "in", ["Open", "Draft"]], ["priority", "=", "High"]](note: this is OR, not AND—use separate lists for AND) - Child table fields:
[["items.item_code", "=", "ABC-001"]](works if you specify the child table field path)
For text search, use ["name", "like", "%keyword%"].
Common Pitfalls and Solutions
Even experienced developers hit a few snags:
- Empty list when you expect data: Check that the DocType name is spelled exactly, and that filters match the field types (e.g., Date fields need string format "YYYY-MM-DD").
- Field not returned: Only fields in the
fieldslist are returned. If you need a field, add it explicitly. - Permission errors:
get_listrespects user permissions by default. If you're running as a user without read access, you'll get an empty list. To bypass permissions (only in server-side scripts where appropriate), passignore_permissions=True. - Large result sets: The default limit is 20. If you need thousands of records, use
limit_page_length=0but be aware of memory usage. Better: usefrappe.get_allwithlimit_page_length=0and process in batches. - Child table data:
get_listdoes not automatically fetch child table rows. You need to query the child DocType separately or usefrappe.get_doc.
Comparison: get_list vs get_all vs get_doc
Choosing the right method matters for performance and clarity:
- frappe.get_list / frappe.get_all: Both are identical in function. Use them when you need a list of records with a subset of fields. They are fast and lightweight.
- frappe.get_doc: Loads a single document as an object with all fields, child tables, and methods. Use when you need to manipulate or validate the document, not just read a few columns.
- frappe.db.sql: Raw SQL query. Use only when you need complex joins or aggregations that the ORM doesn't support easily. Always use parameterized queries to avoid SQL injection.
In practice, if you're writing a server script that needs to iterate over records, get_list is almost always the right choice.
Real-World Examples
Here are a few concrete use cases you'll encounter:
- Export a list of customer emails for a newsletter (assuming you have permission):
customers = frappe.get_list("Customer", fields=["email_id"], filters={"email_id": ["!=", ""]}) for c in customers: print(c.email_id) - Count open tasks for a project:
count = frappe.get_list("Task", filters={"project": "PROJ-001", "status": "Open"}, limit_page_length=0) print(len(count)) - Get all items with stock below reorder level (using a custom field):
items = frappe.get_list("Item", filters=[["actual_qty", "<", "reorder_level"]], fields=["name", "actual_qty", "reorder_level"])
Performance Tips
When working with large datasets, keep these in mind:
- Always specify
fieldsto avoid fetching unnecessary columns. - Use
limit_page_lengthto paginate. For bulk operations, loop through pages usingstart. - If you only need names, use
fields=["name"](or just omit fields) to reduce data transferred. - Avoid using
get_listinside a loop over anotherget_listresult—this causes N+1 queries. Instead, fetch all needed data in one query or use joins. - Consider using
frappe.db.get_valuesif you need a simple list of values from one column—it's even faster.
Frequently Asked Questions
What is the difference between frappe.get_list and frappe.get_all?
There is no difference. frappe.get_list is an alias for frappe.get_all. Both are the same function; you can use them interchangeably. The framework maintains both for historical reasons and to make code more readable in different contexts.
How do I get all records without a limit?
Pass limit_page_length=0 to the method. This will return all matching records. However, be careful with very large tables—it may cause memory issues. In those cases, consider paginating with start and limit_page_length.
Can I use frappe.get_list to get child table records?
Yes, but you need to query the child DocType directly. For example, to get all items from a specific Sales Order, you would call frappe.get_list("Sales Order Item", filters={"parent": "SO-00001"}). The get_list on the parent DocType will not automatically include child rows.
Why does frappe.get_list return an empty list even though records exist?
Common reasons: incorrect doctype spelling, filters that don't match (e.g., wrong field type), or permission restrictions. If you're testing in a script that runs as a user with limited access, try adding ignore_permissions=True to see if that's the issue. Also, ensure the field names are correct—use the fieldname (like customer_name) not the label.
How do I get a list of names only?
Simply omit the fields parameter, or pass fields=["name"]. The result will be a list of dicts each with a name key. If you want a plain list of strings, use frappe.db.get_values or after getting the list, extract names with a list comprehension: [d.name for d in frappe.get_list("Customer")].
Is frappe.get_list safe from SQL injection?
Yes, when used correctly. The method uses parameterized queries internally. However, if you pass raw strings in filters, they are properly escaped. Avoid using frappe.db.sql with string concatenation; always use %s placeholders.