Andrius
Andrius

Reputation: 21168

Odoo 8 - records browse equivalent with new api?

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?

Upvotes: 4

Views: 10601

Answers (2)

Elcin
Elcin

Reputation: 11

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

Upvotes: 1

Ludwik Trammer
Ludwik Trammer

Reputation: 25052

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)

Upvotes: 5

Related Questions