1
votes

I want to interactively create new Odoo/OpenERP product. I got as far as importing the addon:

python

>>> import openerp
>>> openerp.tools.config.parse_config(['--addons-path=addons'])
>>> from openerp.addons.product import product
>>> p = new product.product_product()
SyntaxError: invalid syntax

>>> p = product.product_product()
>>> type(p)
NoneType   # no luck here either

# And then there is...

>>> product.product_product.create()

TypeError: unbound method create() must be called with product_product instance as first argument (got nothing instead)

# Ok I get that, but how do I create a product_product instance?

# And this one is not very clear either:

>>> product.product_product.create_instance()

TypeError: create_instance() takes exactly 3 arguments (1 given)

But the create_instance docstring is not very helpful unfortunately and ack-grep create_instance doesn't give me any results inside the addons directory.

I've been looking for good osv (the Odoo ORM) examples, but not much luck there thus far.

3

3 Answers

2
votes

ERPpeek makes this a lot easier.

$ sudo pip install erppeek
$ erppeek --server ... -d ... -u ... -p ... --verbose

>>> prod = model('product.product')
>>> prod.create({ 'name': 'Test Produkt'})

ERPpeek screenshot

1
votes

OpenERP's "ORM" is a little bit tricky, and poorly documented.

However, we can read sources and understand, what kind of magic to expect:

# 1. Set modules path, like done above. 
import openerp
openerp.tools.config.parse_config(['--addons-path=addons'])

# 2. Obtain cursor and pool of models. 
db, pool = openerp.pooler.get_db_and_pool('dbname')

# 3. Import the model, that you want to create, like done above. 
from openerp.addons.product import product

# 4. Initialize database cursor
curr = db.cursor()

# 5. Create an instance of the model
p = product.product_product.create_instance(pool, curr)

# 6. Now create a model with values
product.product_product.create(p, curr, 1, {'name':'Test2'})
0
votes

One more way to work with OpenERP/Odoo orm interactively and remotely is to use Openerp Proxy lib/cli. It has IPython shell, and tools to work with OpenERP/Odoo data interactively.

In Your at first install and run openerp_proxy shell:

$ pip install openerp_proxy
$ openerp_proxy

then connect to database:

>>> db = session.connect()  # all connection related info will be asked here

and create product:

>>> product_obj = db['product.product']  # get product model
>>> product_id = product_obj.create({'name': 'My cool product'})

(for more info read the docs)

(And in future versions some syntax sugar will be implemented)