1
votes

Hi guys i am new to Odoo, for now i have 2 model as below:

class HumanResource(models.Model):
# _name = 'hr.employee'
_inherit = 'hr.employee'

food_ids = fields.One2many(
    'hr.employee.food',
    'food_id',
    string='Food Cost'
)


class HrFood(models.Model):
_name = "hr.employee.food"
_description = "Food"

food_id = fields.Many2one('hr.employee', string='Food', default=lambda self: self.env['hr.employee'].id)
# food_id = fields.Many2one('hr.employee', 'Food')
# foodtype = ?to?
food_name = fields.Char(
    string='Food Name',
    help='Please Enter the Food Name'
)

food_category = fields.Selection(
    [('breakfast', 'Breakfast'),
     ('lunch', 'Lunch'),
     ('teatime', 'Tea Time'),
     ('dinner', 'Dinner'),
     ('supper', 'Supper')],
    string='Category',
)
food_cost = fields.Float(
    string='Food Amount',
    digits=(5, 2)
)

I'm inheriting the existing hr.employee model that i have installed in the "Apps".

Below is the inherit view. enter image description here

Then what i want is when i click on "Add an Item", it will automatically fills in the employee ID i have select into the food_id (Many2one fields) in HrFood class.

This is the current result, and the field i've circle up is the field i want to set the default as the employee i've selected. enter image description here

Please help me to solve my question, i am newbee in Odoo. And my Odoo version is Odoo 11, Thanks in advance.

3

3 Answers

1
votes

You don't need to fill the field food_id because Odoo will handle that for you. Just make that field invisible, so the user don't bothers to fill it.

Some advices:

  1. Rename food_id to employee_id, because actually behind this attribute there is an employee.
  2. Define a editable tree view in your employee form view for your food entries. For those 3 columns, a form view for the food entries is just too much.
1
votes

You can add a default_get method in HrFood class to get the current employee id that is selected.

In your XML field add this following line:

<field name='food_ids'  context="{'default_active_id': active_id}"></field>

then in Python add the function in HrFood Class

 @api.model
 def default_get(self, fields):
     res = super(HrFood, self).default_get(fields)
     if self.env.context.get('default_active_id'):
        food_id = self.env.context.get('default_active_id')
     return res.update({'food_id': food_id})
0
votes

I used this

with metohd: fields_view_get.

When I selecting one row in the treeview, then it opens the popup and there in the context is parent models id, not the selected rows model id.

I think that the reason is because the record data is populated to the selected rows model after than when fields_view_get is invoked.