This question has been flagged
2 Replies
9129 Views

I am in process of writing OpenERP modules and want to learn how:

(1) To be able to create model objects

(2) Store data or records in them via an API.

(3) Query them to retrieve data. (Just like we do in Django Models using the Queryset API)

Where can I get a good tutorial that explains the above 3 cases with all the available methods? Also, I want to interact with the ORM with the help of easy to use Python API's without using XML-RPC's as much as possible.

Avatar
Discard
Best Answer

Regarding (1): when you do a search for 'openerp create module', there are various sites (including youtube) which show and help you to create a new module. Although many of them say it is for V6 or V6.1, it also works for V7.

Regarding (2): What do you mean by this question?

Regarding (3): within the module, you can use API-like things like

model_id = model.search([('fieldname', '=', some_value')])

if there is no result, model_id will be [], otherwise [<id>]

It is not clear to me what you mean by the last sentence, but I think you want to use the openERP ORM, from script(s), without logging in into openERP. In that case, I use openerplib, a module you can install 'openerplib'. This allows you to hide the XML-RPC like code, to get some nicer looking code. Next week I can post how to install this library and an example, how to make a script using this library.

#openerplib I use Ubuntu, so if you are using Windows, change the commands to Windows commands.

To install openerplib, you need to download and install setup_tools:

wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
sudo ./setuptools-0.6c11-py2.7.egg

now you can openerplib:

sudo easy_install openerp-client-lib

usage of openerplib in a script

import openerplib
h = 'localhost'
db = 'myDB'
u = 'admin'
pw = 'verySecurePassword!'

connection = openerplib.get_connection(
    hostname=h, database=db, login=u, password=p)

#now you have a connection to the DB and can you do some things
#first get an object of your model
user_obj = connection.get_model('res.model')
# use this object to execute some action, in this case, search for user(s)
# with John in the name, but also Johnny....
user_id = user_obj.search([('name', 'in', 'John')])
if user_id == []:
    print 'No user found who has John in his name'
else:
    #now read all the users whose IDs are in user_id
    users = user_bj.read(user_id)
    print 'There are %s user(s) with John in the name' %(len(user_id)
    for u in users:
        print 'User %s has userID %s' %(u['name'], u['id'])

#close the connection nicely
connection.close()
Avatar
Discard
Author

(2) I want to load a model (table) with data retrieved dynamically from an external application API. The idea is to be able to interact with the external API from within OpenERP, synchronize OpenERP model data with the API data, (when a new record is created via Openerp action, a resource is added for the same via a PUT or POST request to the external API too, and vice versa)

Author

Perhaps (2) needs to be framed into a new question. The openerplib thing sounds exiting. Can you give me a link to it; get lots of stuff when I search?

Best Answer

Hi,

you can start with Developer Memento OpenERP for Open Source RAD with OpenERP 7.0 and good start with ORM.

Avatar
Discard
Author

Yes, got it a day back. I got a link on one of your other answers on a question asking about Module development. This doc is perhaps the best I have see so far. Thanks, (On a sidenote, I still think there needs to be more documentation to explain module development in detail and with different examples)