Data not coming in Virtual DocType

Hello,

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 my_test.py.

# 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):
		pass

	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(self.name)
		return super(Document, self).__init__(d)

	def db_update(self):
		pass

	def get_list(self, args):
		pass

	def get_count(self, args):
		pass

	def get_stats(self, args):
		pass

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?

TIA

Yogi Yang

Hello,

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?

TIA

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:

https://frappeframework.com/docs/v14/user/en/basics/doctypes/virtual-doctype

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

2 Likes

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