This question has been flagged
1 Reply
4176 Views

I have a custom module that has stored project_is in a Many2one field and I need to fill those projects in the new Many2many relation field. For this I'm trying to use a method, but it throws exception.

Exception:

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly 
[object with reference: name - name]

My custom module:

    _name = 'pvz.hours'

    partner_id = fields.Many2one('res.partner', string='Contact', required=True, ondelete='cascade')   # Stik
    project_id = fields.Many2one('project.project', string='Project', ondelete='cascade')
    project_ids = fields.Many2many(
        comodel_name='project.project',
        relation='project_pvz_hours_rel',
        column1='pvz_hours_id',
        column2='project_id',
        string='Project'
    )

The method that throws the above exception (with no try catch):

    def transfer_project_id(self):
       all_pvz_data = self.env['pvz.hours'].sudo().search([])
        for data in all_pvz_data:
            if data.partner_id:
                if len(data.project_ids.ids) > 0:
                    continue
                try:
                    data.project_ids = [(6, 0, [data.project_id.id])]
                except Exception as e:
                    raise ValidationError(u'ERROR on project id transfer! \n{}'.format(e))

The problem is that project has no name filed in DB and I just need to fill the Many2many relation with the projects that are now in the Many2one relation.

Is there any other way I can transfer data between the relation fields?

Avatar
Discard
Author Best Answer

Damn I wasted 1 hour googling for a error that I made in the code. The problem was in the if clause, because I tried pass project_id from a record that had no project_id.  I was checking if the partner_id record existed and not the project_id! 

Fixed:

    def transfer_project_id(self):
        all_pvz_data = self.env['pvz.hours'].sudo().search([])
        for data in all_pvz_data:
            if data.project_id:
                if len(data.project_ids.ids) > 0:
                    continue
                try:
                    data.project_ids = [(6, 0, [data.project_id.id])]
                except Exception as e:
                    raise ValidationError(u'ERROR on project id transfer! \n{}'.format(e))
Avatar
Discard