Question

I have a table that looks like this:

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'),
       .
       .
       .

The required=True is required by OpenERP (if it's not there, OE adds it).

When I use the web interface I am able to create a new shipping record and pick existing partners; however, if I try the same thing using XML-RPC (supplying the partner_ids in the shipping record create call) OpenERP tries to create a new record in res.partner using default settings, which of course fails because some required fields have no default (such as the name).

Here's the XML-RPC code I'm using:

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)

I have verified that the ids being passed are correct.

Is this a bug in my code, or in OpenERP?

Was it helpful?

Solution

In browsing through orm.py I found this:

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.
    """

So I tried supplying the already existing partner_id in the context dict as

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

I consider the whole mess a bug in OpenERP, but at least there is a not-horrible work-a-round.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top