peterg
June 10, 2021, 7:06pm
2
Virtual Doctypes aren’t really working yet, or at least it’s not clear to me how to get them to work. There’s some discussion and fixes in the pipeline, but not clear when it will all be merged:
frappe:develop ← zerodha:virtual_doctype
opened 11:33AM - 23 Dec 20 UTC
## Summary
Virtual DocType is a feature-extension for DocType which allows de… velopers to create DocTypes with custom data sources and DocType controller. The purpose is to define custom DocTypes in the system without creating a table in the database, while utilizing the frontend, resource APIs, and roles and permissions from the framework.
These Virtual DocTypes function exactly like normal DocTypes in the frontend and are indistinguishable for the end-user, but gives more control to the developer over the DocType's data source. With this, the data source for a Virtual DocType can be anything: an external API, a secondary database, JSON or CSV files, etc. This enables the developers to plug-in database backends other than MariaDB and Postgres, and makes the Frappe Framework even more powerful!
## Usage
### Creating a Virtual DocType
To create a Virtual DocType, just select the **Virtual DocType** checkbox while creating the DocType:

### Creating a Custom Controller
As an example, the following controller code uses a JSON file as the DocType datasource:
```python
class test_virtual(Document):
def db_insert(self):
d = self.get_valid_dict(convert_dates_to_str=True)
with open("data_file.json", "w+") as read_file:
json.dump(d, read_file)
def load_from_db(self):
with open("data_file.json", "r") as read_file:
d = json.load(read_file)
super(Document, self).__init__(d)
def db_update(self):
d = self.get_valid_dict(convert_dates_to_str=True)
with open("data_file.json", "w+") as read_file:
json.dump(d, read_file)
def get_list(self, args):
with open("data_file.json", "r") as read_file:
return [json.load(read_file)]
```
To integrate other datasources with the Virtual DocType, you will need to add controller methods defining the database access.
### Outcome
* The frontend for Virtual DocTypes remain unchanged:

* All the `/api/resource` methods defined by the framework are compatible with Virtual DocTypes.
---
Documentation Link: https://github.com/frappe/frappe_docs/pull/71