Inserting records

Hi All,

I’m writing a custom service to insert data from another DB into my ERPNext site. I’m trying to initialise my site’s frappe config to perform database queries, without having to go using bench execute.

This script is being run from a module within one of my frappe apps (frappe-bench/apps/myapp)

`
import frappe

def main():
frappe.init(“/home/frappe/frappe-bench/sites/mysite”)
frappe.connect()
print frappe.get_conf()
todo = frappe.get_doc({“doctype”:“ToDo”, “description”: “testing”})
todo.insert(ignore_permissions=True)
print “Success”

if name == ‘main’:
main()
`

Running this with python import.py yields the following output:
Starting up... {u'db_password': u'G4WrUSfhwpzOydd5', u'db_name': u'43b4a77586', u'developer_mode': 1, u'limits': {u'space_usage': {u'database_size': 8.92, u'files_size': 3.0, u'total': 13.92, u'backup_size': 2.0}}} Success

However, my ToDo record is not stored in the database.

Also, mysite is running via bench start

Any ideas?

Thanks!

are you planning to run the script remotely or inside the server?

Inside the server.

You do not need to put this code

frappe.init("/home/frappe/frappe-bench/sites/mysite")
frappe.connect()

If you want to run your code in command you can do this

bench --site example.com execute <script>

1 Like

I attempted this, and whilst it does work in isolation, it stopped working when I incorporated the insert() into an asynchronous Tornado server.

As part of the debugging process, I’d like to eliminate bench as being the potential issue, by instantiating the frappe db connection manually.

So I’d really like to know how to manually instantiate frappe inserts as above.

Thanks!

So if that’s the case you have to use GitHub - frappe/frappe-client: Python library to use Frappe API
for me it is consider as a external app.

Great, thanks for this.

For performance reasons, my goal is to avoid using the REST API. But I will use it as a fallback.