Odoo Help

0

How to change a boolean field based on another field? (when a many2one is created)

By
Skander
on 1/20/16, 10:54 AM 821 views

Hello everyone, i'm new to odoo developing and this is my first module.

I want to assign material resources to project task.(most of the time vehicles, we have many)

I want to know which cars are free and which ones are assigned to tasks.

So i created a boolean field named assigned to be True when a material resource is added to a task(i.e task_id is no more null), but it is always false when i check the database.So the function i created seems to not work for some reason.

from openerp import models, fields, api
class MaterialResource(models.Model):
    _name = 'material.resource'
    name = fields.Char('Material Resource', required=True)
    description = fields.Selection(string='Description', selection=[('m', 'Mazda'), ('f', 'Ford')])
    task_id = fields.Many2one("project.task", inverse_name="material_resource_ids")
    assigned = fields.Boolean(compute="_get_material_status", store="True")
    @api.one
    @api.depends('task_id')
    def _get_material_status(self):
        if self.task_id is None:
            return False
        else:
            return True
class TaskMaterial(models.Model):
    _inherit = 'project.task'
    material_resource_ids = fields.One2many(comodel_name="material.resource", inverse_name="task_id", string="Material Resource")
1
Akhil P Sivan
On 1/21/16, 4:25 AM

Hi,

Try like this:

from openerp import models, fields, api

class MaterialResource(models.Model):

_name = 'material.resource'

name = fields.Char(string='Material Resource', required=True)

description = fields.Selection(string='Description', selection=[('m', 'Mazda'), ('f', 'Ford')])

task_id = fields.Many2one("project.task", string="Task")

assigned = fields.Boolean(compute="_get_material_status", string="Assigned")

@api.one

@api.depends('task_id')

def _get_material_status(self):

if self.task_id:

self.assigned = True

else:

self.assigned = False

class TaskMaterial(models.Model):

_inherit = 'project.task'

material_resource_ids = fields.One2many("material.resource",'task_id', string="Material Resources")

1

Try following:

@api.one    
@api.depends('task_id')
def _get_material_status(self):
if self.task_id:
self.assigend=True
    else:
        self.assigned=False
   



 

0
Skander
On 1/21/16, 10:38 AM

Thank you i have tried all of these but it's still False.

task_id takes numbers (1,2,3...) which is normal when i execute select * from material_resource;

but when i test it in the view, <field name="task_id"/> shows the name of the task it's very strange i don't understand!

Anyway i've found another solution: i create a filter ( task_id is not set) which syntax is domain="[[u'task_id', u'=', False]]"

i can get the same result with it.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 1/20/16, 10:54 AM
Seen: 821 times
Last updated: 1/21/16, 10:38 AM