18

What I've done:

I have a module with

myfield = fields.Many2one('res.partner', string="Graduate", domain=[('is_graduated', '=', True)])

Then I have another class with

_inherit = 'res.partner'
is_graduated = fields.Boolean("Graduated before?", default=False)
graduations = fields.Many2many('my_module.courses', string="Graduation courses")

What I get:

The myfield works good, but the graduations field is empty. If you edit user 1 profile you can add entries to graduation field using Add item, but I need it to be filled automaticaly.

What I expect:

I expect that every record where myfield is set to lets say user 1, will be visible in field graduations when you open user 1 profile. When I create record and set myfield value to lets say user 1, that record must to be visible in user 1 profile in the field graduations. How to achieve that?

hockeyman
  • 1,141
  • 6
  • 27
  • 57
  • And where does the `is_graduated` field come into play? You don't mention it in the "What I expect" section. Also, what the `my_module.courses` model is supposed to represent? It's hard for me to understand what you are trying to achieve... – Ludwik Trammer Aug 15 '15 at 10:25

4 Answers4

40

user_rel_ids = fields.Many2many(comodel_name='course', relation='user_course_rel', column1='user_id', column2='course_id')

Or

user_rel_id = fields.Many2many('course') 

For Filling Data (for add new relation)

user_rel_id = [(4,course_id)]

According to http://odoo4u.blogspot.com/2014/10/orm-methods.html, It says: A full list of options is in the documentation for the class. This same thing will apply for one2many

For a many2many and one2many field, a list of tuples is expected. Here is the list of the tuple that is accepted, with the corresponding semantics:

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3, ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • Excuse me, but what is the object name of the relation table i have just created? i want to use http.request.registry['name_of_relation_table'] to modify the relation table... – Summer Sun Apr 21 '16 at 09:23
1

You need to use an onchange method for myfield, then inside it you need to fill the graduations field, something like this:

@api.onchange('myfield'):
def _onchange_myfield(self):
    #fill graduations field here...
    ...
Juan Salcedo
  • 1,598
  • 1
  • 16
  • 28
  • But the `graduations` field is in different form at all, so maybe there is any other method to automatically fill the values? Like creating a relation or something? – hockeyman Aug 07 '15 at 06:35
  • 1
    like you say: If you edit `user 1` profile you can add entries to `graduation` field using `Add item`, you need to do this operation inside `onchange method` above, and if you need to get access to another model you should use `self.env.['other_model']`, in this case other_model is 'res.partner'. I hope this can be useful for you! Let me know your discharges... – Juan Salcedo Aug 07 '15 at 14:21
1
_inherit = 'crm.phonecall'    
alarm_ids = fields.Many2many('calendar.alarm',string="Reminders") 
 set the alarm_ids of calendar.event model in create method of crm phonecall...
 alarm_ids = [(6,0,self.alarm_ids.ids)] 

_inherit = 'calendar.event'
 alarm_ids = fields.Many2many('calendar.alarm',string="Reminders") 
Bhautik
  • 21
  • 3
0

You can achieve like these.

For example:

@api.one
@api.depends(
      #here you may define your depend field name
)
def _set_graduations(self):
    #here comes your logic which will collect ids
    #and than return it with self.field_name like

    self.graduations = [list_of_ids]


graduations = fields.Many2many('my_module.courses', string='Payments',
                                compute='_set_graduations') 

If you don't want to use @api.depends than you may use @api.multi. For reference you may check out account module with account_invoice.py file.

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
  • And could you help me with logic to collect ids? As I mentioned before, the ids selected should be where `is_graduated` is set to `True`. How could I achieve that? – hockeyman Aug 13 '15 at 07:10
  • you may try with this `self.env['model.name'].search([('field_name', '=', Value)])` now in your case you need to do like `self.env['res.partner'].search([('is_graduated', '=', True)])` – Bhavesh Odedra Aug 13 '15 at 07:16
  • If I do lets say `self.graduations = self.env['res.partner'].search([('is_graduated', '=', True)])` I get internal error – hockeyman Aug 13 '15 at 07:30
  • may you write that error? and just try to solve it out some part buddy. I just write code in air. – Bhavesh Odedra Aug 13 '15 at 07:32
  • Compute doesn't work on many2many fields as far as I know. Won't get triggered – Loïc Faure-Lacroix Feb 06 '16 at 20:13