如何以交互方式在Python中创建Odoo / OpenERP产品实例?

时间:2014-07-14 17:56:03

标签: python openerp odoo

我想以交互方式创建新的Odoo / OpenERP产品。我得到了导入插件:

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)

但遗憾的是create_instance文档字符串不是很有用,ack-grep create_instance并没有在addons目录中给我任何结果。

我一直在寻找好osv(Odoo ORM)的例子,但到目前为止运气不大。

3 个答案:

答案 0 :(得分:2)

ERPpeek让这更容易。

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

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

ERPpeek screenshot

答案 1 :(得分:1)

OpenERP的“ORM”有点棘手,记录不完整。

然而,我们可以阅读资料并了解,期待什么样的魔力:

# 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'})

答案 2 :(得分:0)

以交互方式和远程方式使用OpenERP / Odoo orm的另一种方法是使用Openerp Proxy lib / cli。它具有IPython shell,以及交互式处理OpenERP / Odoo数据的工具。

在您第一次安装并运行openerp_proxy shell时:

$ pip install openerp_proxy
$ openerp_proxy

然后连接到数据库:

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

并创建产品:

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

(欲了解更多信息,请阅读docs

(在未来的版本中,将实现一些语法糖)