Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
7873 Vizualizări

I'm trying to get the partner_id upon updating the form, unfortunately I can't capture it. I've added a def write function in order to modify the updating. What I wanted to do is, when the form is submitted and the field "state" is equals to approved, it will create another record at other table/object. But I can't seem to make it work and capture the partner id using vals['partner_id'] which is also in the form..Please help and thanks in advance

class res_processing_request(osv.osv):
    _name = 'res.processing.request'
    _description = 'res.processing.request'

    _columns = {
        'date': fields.datetime('Date Requested', required=True),
        'partner_id': fields.many2one('res.partner', 'Client', required=True),
        'status': fields.many2one('res.partner.application.status', 'Status'),
        'name': fields.char('Request Name', size=255, required=True),
        'requestor_id': fields.many2one('res.users', 'Requested By', required=True),
        'approver_id': fields.many2one('res.users', 'Approved By'),
        'state': fields.selection([
            ('For Approval', 'For Approval'),
            ('On-hold', 'On-hold'),
            ('Approved', 'Approved'),
        ], 'State', required=True),
        'active': fields.boolean('Active'),
    }
    _defaults = {
        'active': lambda *a: 't',
        'state': lambda *a: 'For Approval',
    }

    def write(self, cr, uid, ids, vals, context=context):
        if vals['state'] == 'Approved':
            # processing_request = self.read(cr, uid, ids, context=context)

            application_history_data = {
                'create_uid' : uid,
                'create_date' : time.strftime('%Y-%m-%d %H:%M:%S'),
                'status' : 348,
                'user_id' : uid,
                'email' : '',
                'subject' : '',
                'date' : time.strftime('%Y-%m-%d %H:%M:%S'),
                'partner_id' : vals['partner_id'],
                'visible' : 1
            }

            self.pool.get('res.partner.application.history').create(cr, uid, application_history_data)

        return super(res_processing_request, self).write(cr, uid, ids, vals, context)

    def onchange_status(self, cr, uid, ids, status_id, context={}):
        if status_id:
            status = pooler.get_pool(cr.dbname).get('res.partner.application.status').browse(cr, uid, status_id, context)
            return {'value': { 'name': status.name }}

res_processing_request()

Imagine profil
Abandonează

Weird. It should be there. Maybe the xml file associated with the model is the erroneous one?

Cel mai bun răspuns

Hello Jason,

You are doing right to create a another record if state is approved.

I think you didn't understood the working flow of the write method.
Write method will have only those records in vals which are being altered/updated.

So the reason you are not getting partner id in vals is that, you are not changing the value of partner_id field.

Instead of using vals, you should first call the method of the super class so that all the changes will get affect in database and then browse the records.

Try this:

def write(self, cr, uid, ids, vals, context=context):
    res = super(res_processing_request, self).write(cr, uid, ids, vals, context=context)
    for rec in self.browse(cr, uid, ids, context=context):
        if rec.state == 'Approved':
            application_history_data = {
                'create_uid' : uid,
                'create_date' : time.strftime('%Y-%m-%d %H:%M:%S'),
                'status' : 348,
                'user_id' : uid,
                'email' : '',
                'subject' : '',
                'date' : time.strftime('%Y-%m-%d %H:%M:%S'),
                'partner_id' : rec.partner_id and rec.partner_id.id,
                'visible' : 1
            }
            self.pool.get('res.partner.application.history').create(cr, uid, application_history_data, context=context)
    return res

Imagine profil
Abandonează