Data not coming in Virtual DocType


I have create a Virtual DocType My Test.

In this I want to load data from it from database. Here is the code in the file

# Copyright (c) 2022, Yogesh and contributors
# For license information, please see license.txt

# import frappe
import frappe
from frappe.model.document import Document
from frappe import _

class MyTest(Document):
	def db_insert(self):

	def load_from_db(self):
		data = frappe.db.sql(""" SELECT `tabPurchase Receipt`.name, `tabPurchase Receipt`.supplier_name, `tabPurchase Receipt`.posting_date, `tabPurchase Receipt Item`.item_name, `tabPurchase Receipt Item`.received_qty, `tabPurchase Receipt Item`.rejected_qty, `tabPurchase Receipt Item`.warehouse, `tabPurchase Receipt Item`.purchase_order, `tabPurchase Receipt Item`.batch_no, `tabPurchase Receipt Item`.coil_heat_no, `tabPurchase Receipt Item`.quality_inspection, `tabPurchase Receipt Item`.coil_qty, `tabPurchase Receipt Item`.coil_wt FROM `tabPurchase Receipt`, `tabPurchase Receipt Item` WHERE `tabPurchase Receipt Item`.parent = `tabPurchase Receipt`.name; """, as_dict=1)
		d = data.get(
		return super(Document, self).__init__(d)

	def db_update(self):

	def get_list(self, args):

	def get_count(self, args):

	def get_stats(self, args):

When I load the page I am getting error message.

AttributeError: 'list' object has no attribute 'get'

What mistake am I making and how to solve it?


Yogi Yang


After a few experiments I think I will have to convert the output of frappe.db.sql to JSON.

How to convert the output to JSON?


Yogi Yang

you can use json.dumps()

You’re trying to use .get on the output of frappe.db.sql which is always a list or None.

There is an example of virtual doctype usage in docs:

And better “real world” usage here: feat: Improved RQ monitoring and control with virtual doctypes by ankush · Pull Request #18086 · frappe/frappe · GitHub


Hi! Should be working a link field pointing from another doctype to virtual doctype? Is link feature using virtual doctype controller methods?