XML-RPC自动创建记录,而Web界面则不会

时间:2013-11-13 23:57:16

标签: python openerp xml-rpc

我有一个看起来像这样的表:

class fnx_sr_shipping(osv.Model):
    _name = 'fnx.sr.shipping'
    _description = 'shipping & receiving entries'
    _inherits = {
       'res.partner': 'partner_id',
       }
    _order = 'appointment_date desc, appointment_time asc'

    _columns = {

       .
       .
       .
       'partner_id': fields.many2one(
            'res.partner',
            'Partner',
            required=True,
            ondelete='restrict'),
       .
       .
       .

OpenERP需要required=True(如果不存在,OE会添加它)。

当我使用网络界面时,我可以创建新的运输记录并选择现有的合作伙伴;但是,如果我使用XML-RPC尝试相同的事情(在发货记录创建调用中提供partner_id),OpenERP会尝试使用默认设置在res.partner中创建新记录,这当然会失败,因为某些必填字段没有默认值(例如name)。

这是我正在使用的XML-RPC代码:

import openerplib
OE = PropertyDict()          # allows attribute-style access for keys
OE.conn = openerplib.get_connection(
        hostname='xxx',
        database='yyy',
        login='zzz',
        password='...'
OE.res_partner = conn.get_model('res.partner')
.
.
.
values['partner_id'] = 77     # or whatever it actually is ;)
OE.fnx_shipping.create(values)

我已经确认传递的id是正确的。

这是我的代码或OpenERP中的错误吗?

1 个答案:

答案 0 :(得分:0)

在浏览orm.py时,我发现了这个:

def name_create(self, cr, uid, name, context=None):
    """Creates a new record by calling :meth:`~.create` with only one
       value provided: the name of the new record (``_rec_name`` field).

       The new record will also be initialized with any default values applicable
       to this model, or provided through the context.

       The usual behavior of :meth:`~.create` applies.
       Similarly, this method may raise an exception if the model has multiple
       required fields and some do not have default values.
    """

所以我尝试将partner_id dict中现有的context提供为

OE.fnx_shipping.create(values, context={'default_partner_id':partner_id})

我认为整个混乱是OpenERP中的一个错误,但至少有一个非常糟糕的工作循环。