DB Changes Through Python Script

194 views
Skip to first unread message

Brendan Kiu

unread,
Aug 13, 2014, 4:08:13 AM8/13/14
to erpnext-dev...@googlegroups.com
I'm working on writing some import scripts for large datasets, and I noticed that using the python shell through `frappe [site_name] --python` doesn't actually commit database changes through frappe.insert(). I would like to put my files on the server and run my commands through the terminal. Any way I can do that? 

Anand Doshi

unread,
Aug 13, 2014, 4:59:03 AM8/13/14
to ERPNext Developer's Forum
Run frappe.db.commit() to commit :)

-Anand.


On Wed, Aug 13, 2014 at 1:38 PM, Brendan Kiu <bren...@gmail.com> wrote:
I'm working on writing some import scripts for large datasets, and I noticed that using the python shell through `frappe [site_name] --python` doesn't actually commit database changes through frappe.insert(). I would like to put my files on the server and run my commands through the terminal. Any way I can do that? 

--
Note:
 
If you are posting an issue,
 
1. ERPNext is a free and open source software and support is given on this forum by a team (https://frappe.io/webnotes). So please consider donating if you find this forum useful (https://frappe.io/buy). Even a small amount would be helpful.
2. We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
3. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
4. For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
 
End of Note
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/e0a6b192-c2d1-4d25-b47b-f6527073f575%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brendan Kiu

unread,
Aug 13, 2014, 5:27:02 AM8/13/14
to erpnext-dev...@googlegroups.com
Cool, thanks! I'm guessing this gets run automatically when running from the site, so I don't need to do it when handling HTTP requests.


On Wednesday, August 13, 2014 2:29:03 PM UTC+5:30, Anand Doshi wrote:
Run frappe.db.commit() to commit :)

-Anand.
On Wed, Aug 13, 2014 at 1:38 PM, Brendan Kiu <bren...@gmail.com> wrote:
I'm working on writing some import scripts for large datasets, and I noticed that using the python shell through `frappe [site_name] --python` doesn't actually commit database changes through frappe.insert(). I would like to put my files on the server and run my commands through the terminal. Any way I can do that? 

--
Note:
 
If you are posting an issue,
 
1. ERPNext is a free and open source software and support is given on this forum by a team (https://frappe.io/webnotes). So please consider donating if you find this forum useful (https://frappe.io/buy). Even a small amount would be helpful.
2. We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
3. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
4. For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
 
End of Note
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Rushabh Mehta

unread,
Aug 13, 2014, 5:38:16 AM8/13/14
to erpnext-dev...@googlegroups.com
Cool, thanks! I'm guessing this gets run automatically when running from the site, so I don't need to do it when handling HTTP requests.

Commits happen on "POST" / "PUT" requests only!

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/f0bf1283-63ab-4982-a89b-09552499c076%40googlegroups.com.

Brendan Q

unread,
Aug 13, 2014, 7:59:34 AM8/13/14
to erpnext-dev...@googlegroups.com
Cool, thanks for the pointers. I've got an external script, import_data.py, which uses the frappe object and is meant to be executed in the python terminal for my site. When I execute `env/bin/python import_data.py` I get the error 

NameError: name 'frappe' is not defined 

How do I import the frappe object from my external script, as well as set the site? Thanks.


Brendan Kiu


You received this message because you are subscribed to a topic in the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/erpnext-developer-forum/uQI-jMQDXTA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/4BBA8123-2BE0-445D-92FC-5FAEF3FBE4D7%40gmail.com.

Anand Doshi

unread,
Aug 13, 2014, 10:01:45 AM8/13/14
to ERPNext Developer's Forum
well you need to activate bench environment to access frappe.
Go to frappe-bench and run:
. ./env/bin/activate

Then run your script and you should be able to access frappe.

-Anand.


Brendan Q

unread,
Aug 14, 2014, 12:10:11 AM8/14/14
to erpnext-dev...@googlegroups.com
Yep, that worked for me. What's the method to bind the script to a certain site? I get this error now:

    raise RuntimeError('no object bound to %s' % self.__name__)

RuntimeError: no object bound to session

In my script I don't specify which site to use, so I'm guessing that needs to happen before I can access the frappe.new_doc(), etc methods.


Brendan Kiu


Brendan Kiu

unread,
Aug 14, 2014, 12:54:00 AM8/14/14
to erpnext-dev...@googlegroups.com
More specifically,

Traceback (most recent call last):                                                       

  File "import_children.py", line 84, in <module>                                        

    child = frappe.new_doc("My Doctype")                                                  

  File "/home/linuxuser/frappe-bench/apps/frappe/frappe/__init__.py", line 335, in new_do

c                                                                                        

    return get_new_doc(doctype, parent_doc, parentfield)                                 

  File "/home/linuxuser/frappe-bench/apps/frappe/frappe/model/create_new.py", line 17, in

 get_new_doc                                                                             

    "owner": frappe.session.user,                                                        

  File "/home/linuxuser/frappe-bench/env/local/lib/python2.7/site-packages/werkzeug/local

.py", line 338, in __getattr__                                                           

    return getattr(self._get_current_object(), name)                                     

  File "/home/linuxuser/frappe-bench/env/local/lib/python2.7/site-packages/werkzeug/local

.py", line 301, in _get_current_object                                                   

    raise RuntimeError('no object bound to %s' % self.__name__)                          

RuntimeError: no object bound to session                                                 

Thanks!

Anand Doshi

unread,
Aug 14, 2014, 1:24:27 AM8/14/14
to ERPNext Developer's Forum
Hi Brenden,

It would be better if you share your script on gist, so that people can comment at the appropriate place in the code.

-Anand.


--
Note:
 
If you are posting an issue,
 
1. ERPNext is a free and open source software and support is given on this forum by a team (https://frappe.io/webnotes). So please consider donating if you find this forum useful (https://frappe.io/buy). Even a small amount would be helpful.
2. We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
3. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
4. For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
 
End of Note
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.

Brendan Kiu

unread,
Aug 14, 2014, 1:25:15 AM8/14/14
to erpnext-dev...@googlegroups.com
Sure, here it is:
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

rushabh

unread,
Aug 14, 2014, 2:41:06 AM8/14/14
to erpnext-dev...@googlegroups.com
Do

```
frappe.init(sitename)
frappe.connect()
```

Brendan Kiu

unread,
Aug 14, 2014, 4:20:16 AM8/14/14
to erpnext-dev...@googlegroups.com
Yep, that worked. Thanks!

On Thursday, August 14, 2014 12:11:06 PM UTC+5:30, rushabh wrote:
Do

```
frappe.init(sitename)
frappe.connect()
```
Reply all
Reply to author
Forward
0 new messages