This question has been flagged
3 Replies
11090 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")
Avatar
Discard
Best Answer

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

Avatar
Discard
Best Answer

Try following:

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



 

Avatar
Discard
Author Best Answer

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.

Avatar
Discard