4

I read Odoo 8 new api documentation, but could not find it (if there is any). How to directly browse record/records with new api, when you have that models id/ids?

For example let say I want to browse res.partner model and have list of ids: ids = [1, 2, 3].

With old api, you could do it like this:

partners = self.pool.get('res.partner').browse(cr, uid, ids)

With new api, the only way I could think of was to use search as it returns records (not ids as in old api), like this:

partners = self.env['res.partner'].search([('id', 'in', ids)])

This gives same results, but I'm wondering about performance (is it the same?) and convenience (to avoid using search, when you already know ids, just need to browse the records)?

Or search is preferred way in new api to be used when you need to browse records?

Andrius
  • 19,658
  • 37
  • 143
  • 243

2 Answers2

5

You can still use browse with the new api. There is much less reason to do so, because with the new API you usually deal with sets of records, instead of lists of numeric ids. But you absolutely can use browse during the odd times when you actually need it:

partner_ids = [1, 2, 3]
partners = self.env['res.partner'].browse(partner_ids)
Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
  • Thanks, didn't thought it was implemented in new api directly :). Yeah, normally I don't need to browse it, but when dealing with some old methods, sometimes you get ids instead of record sets. So it's more convenient to browse instead of searching for what you already know. – Andrius Feb 05 '15 at 10:12
  • There is a layer of automatic translation. All methods defined using the new API can be called using the old API (with cr, uid, context, ... arguments), and old methods defined using the old API can be called using the new API (without the explicit cr, uid, context, ... arguments). So it doesn't matter whether a particular method had been rewritten for the new API or not (`browse` actually has been rewritten). You can still call it just the same using the new API. – Ludwik Trammer Feb 05 '15 at 10:20
  • Yeah I know that. I was talking about methods that are meant to return ids, not recordsets. For example general method `get_external_id()`, returns dictionary that has `key` as record id and `value` as external id even if you call it using new API. – Andrius Feb 05 '15 at 12:32
1

class Verify_Time(models.Model): _name='verify.time'

@api.model
def default_get(self, fields_name):
    update_ids = []
    data = super(Verify_Time, self).default_get(fields_name)
    if self._context.get('active_id'):
        student_id = self._context.get('active_id')
        for record in self.env['student.student'].browse(student_id):
            for time_record in record.time_table_ids:
                update_ids.append((0,0,{'standared_id':time_record.standared_id.id,
                                        'subject_id':time_record.subject_id.id,
                                        'start_time':time_record.start_time,
                                        'end_time':time_record.end_time}))
    data['s_time_ids'] = update_ids
    return data

s_time_ids = fields.One2many('s.time', 'verify_id', 'Time')
verify = fields.Selection(VERIFY, 'Verified?')

class S_Time(models.Model): _name='s.time'

verify_id = fields.Many2one('verify.time', 'Verify')
serial_no = fields.Integer('#')
standared_id = fields.Many2one('standared.standared', 'Standared')
subject_id = fields.Many2one('section.section', 'Subject')
start_time = fields.Datetime('Start Time')
end_time = fields.Datetime('End Time')
Elcin
  • 11
  • 1