Thursday 3 December 2015

V8 and V7 Field Difference


Here am explaining the field differences between version 7.0 and 8.0

Field in Version8.0


Image



    @api.depends('image')
    def _get_medium_image(self):
        self.image_medium =\
            tools.image_get_resized_images(self.image)['image_medium']

    @api.one
    @api.depends('image')
    def _get_small_image(self):
        self.image_small =\
            tools.image_get_resized_images(self.image)['image_small']

    @api.one
    def _set_image_from_medium(self):
        self.image = tools.image_resize_image_big(self.image_medium)

    @api.one
    def _set_image_from_small(self):
        self.write({'image': tools.image_resize_image_big(self.image_small)})


        image = fields.Binary(string="Image",)    
        image_medium = fields.Binary(compute='_get_medium_image',                                                                            inverse='_set_image_from_medium',
                                 string="Medium-sized image", store=True)
        image_small = fields.Binary(compute='_get_small_image',                                                                                  inverse='_set_image_from_small',
                                string="Small-sized image", type="binary",
                                store=True)



Related Field


Eg:-commercial_partner_id = fields.Many2one('res.partner',string='Partner',related='partner_id.commercial_partner_id',store=True,readonly=True)

Eg:-number = fields.Char(related='move_id.name', store=True, readonly=True,)

Eg:-company_id = fields.Many2one('res.company', string='Company',related='invoice_id.company_id', store=True, readonly=True)

Eg:-is_brand = fields.Boolean(related='sale_product_target_id.is_brand',string='Is Brand',store=True)


Eg:-eccentricity_uom_id = fields.Many2one('product.uom',string="Ref Unit",related='uom_id',readonly=True,)


Eg:-image = fields.Binary(string="Image",related='support_id.image',readonly=True,)
    image_medium = fields.Binary(compute='_get_medium_image',                                                                                inverse='_set_image_from_medium',
                                 string="Medium-sized image", store=True)
    image_small = fields.Binary(compute='_get_small_image', inverse='_set_image_from_small',
                                string="Small-sized image", type="binary",
                                store=True)


     for making an image field as relational field then we have to define normal image field,and          just put the relation in image field only,otherwise no difference.


Functional Field


Example1:



    @api.one

    @api.depends('price_unit', 'discount', 'invoice_line_tax_id', 'quantity',
        'product_id', 'invoice_id.partner_id', 'invoice_id.currency_id')
    def _compute_price(self):
        price = self.price_unit * (1 - (self.discount or 0.0) / 100.0)
        taxes = self.invoice_line_tax_id.compute_all(price, self.quantity, product=self.product_id, partner=self.invoice_id.partner_id)
        self.price_subtotal = taxes['total']
        if self.invoice_id:
            self.price_subtotal = self.invoice_id.currency_id.round(self.price_subtotal)


    price_subtotal = fields.Float(string='Amount', digits= dp.get_precision('Account'),
        store=True, readonly=True, compute='_compute_price')



Example2:



 @api.one

 @api.depends('brand_line.amount','category_line.amount','product_line.amount','saleman_line.amount',
   'customer_line.amount','supplier_line.amount')
    def _compute_amount(self):
        array = []
        self.total_brand_amount = sum((line.p1_amount + line.p2_amount + line.p3_amount + line.p4_amount +
         line.p5_amount + line.p6_amount +line.p7_amount + line.p8_amount + line.p9_amount + line.p10_amount +
        line.p11_amount + line.p12_amount) for line in self.brand_line)
        array.append(self.total_brand_amount)
        array.append(self.total_category_amount)
        array.append(self.total_product_amount)
        array.append(self.total_saleman_amount)
        array.append(self.total_customer_amount)
        array.append(self.total_supplier_amount)
        largest = max(array)
        self.planned_target = largest
        length = len(array)
        largest = array[0]
        for i in range(0,length):
           if array[i] > largest:
             largest = array[i]
             self.planned_target = largest
    total_brand_amount = fields.Float(string='Amount Brand',
        store=True, readonly=True, compute='_compute_amount')



Example3:



    @api.one
    @api.depends(
        'move_id.line_id.account_id',
        'move_id.line_id.reconcile_id.line_id',
        'move_id.line_id.reconcile_partial_id.line_partial_ids',
    )
    def _compute_move_lines(self):
        # Give Journal Items related to the payment reconciled to this invoice.
        # Return partial and total payments related to the selected invoice.
        self.move_lines = self.env['account.move.line']
        if not self.move_id:
            return
        data_lines = self.move_id.line_id.filtered(lambda l: l.account_id == self.account_id)
        partial_lines = self.env['account.move.line']
        for data_line in data_lines:
            if data_line.reconcile_id:
                lines = data_line.reconcile_id.line_id
            elif data_line.reconcile_partial_id:
                lines = data_line.reconcile_partial_id.line_partial_ids
            else:
                lines = self.env['account.move.line']
            partial_lines += data_line
            self.move_lines = lines - partial_lines



    ##many2many functional field
    move_lines = fields.Many2many('account.move.line', string='Entry Lines',
        compute='_compute_move_lines')




    @api.model
    def _default_account(self):
        if self._context.get('type') in ('out_invoice', 'out_refund'):
            return self.env['ir.property'].get('property_account_income_categ', 'product.category')
        else:
            return self.env['ir.property'].get('property_account_expense_categ', 'product.category')

    #Many2one functional field
    account_id = fields.Many2one('account.account', string='Account',
        required=True, domain=[('type', 'not in', ['view', 'closed'])],
        default=_default_account,
        )


Many2many



    invoice_line_tax_id = fields.Many2many('account.tax',
        'account_invoice_line_tax', 'invoice_line_id', 'tax_id',
        string='Taxes', domain=[('parent_id', '=', False)])
    product_ids = fields.Many2many('product.product',string='Products',)


Many2one


    period_id = fields.Many2one('account.period', string='Force Period',
        domain=[('state', '!=', 'done')], copy=False,
        readonly=True, states={'draft': [('readonly', False)]})


One2many


invoice_ids = fields.One2many('account.invoice', 'partner_id', string='Invoices',
        readonly=True, copy=False)

Boolean Field


    sent = fields.Boolean(readonly=True, default=False, copy=False,
        help="It indicates that the invoice has been sent.")

Date field


    date_invoice = fields.Date(string='Invoice Date')


Text


        note =fields.Text(string='Terms and conditions'),

Char


        name = fields.Char(string='Order Reference',)





Field in Version7.0

Image


    def _get_image(self, cr, uid, ids, name, args, context=None):
        result = dict.fromkeys(ids, False)
        for obj in self.browse(cr, uid, ids, context=context):
            result[obj.id] = tools.image_get_resized_images(obj.image, avoid_resize_medium=True)
        return result

    def _set_image(self, cr, uid, id, name, value, args, context=None):
        return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)


    _columns = {
        'image': fields.binary("Image",help="This field holds the image used as image for the facility, limited to 1024x1024px."),
        'image_medium': fields.function(_get_image, fnct_inv=_set_image,
            string="Medium-sized image", type="binary", multi="_get_image",
            store={
                'od.property': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
            },
            help="Medium-sized image of the Facility. It is automatically "\
                 "resized as a 128x128px image, with aspect ratio preserved, "\
                 "only when the image exceeds one of those sizes. Use this field in form views or some kanban views."),
        'image_small': fields.function(_get_image, fnct_inv=_set_image,
            string="Small-sized image", type="binary", multi="_get_image",
            store={
                'od.property': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
            },
            help="Small-sized image of the Facility. It is automatically "\
                 "resized as a 64x64px image, with aspect ratio preserved. "\
                 "Use this field anywhere a small image is required."),
   }






Related Field


Eg:od_shop_id':fields.related('session_id','config_id',type='many2one',relation='pos.config',string='Shop',store=True,)
Eg:-'exchange_rate':fields.related('currency_id','rate',type='float',string='Exchange Rate',),
Eg:-'fiscalyear_id':fields.related('period_id','fiscalyear_id',string='Fiscal Year',type='many2one',relation='account.fiscalyear'),
Eg:-'company_id': fields.related('journal_id','company_id',type='many2one',relation='res.company',string='Company',store=True,readonly=True)



Functional Field


Example1:

    def _od_shipping_count(self, cr, uid, ids, field_name, arg, context=None):
        res ={}
        for obj in self.browse(cr, uid, ids, context):
            shipment_ids = self.pool.get('od.shipping.doc').search(cr, uid, [('purchar_order_id', '=', obj.id)])
            if shipment_ids:
                res[obj.id] = len(shipment_ids)
        return res


    _columns = {
        'od_shipping_count':fields.function(_od_shipping_count,string='Docs',type='integer'),
    }


Example2:


    def _get_management_line(self, cr, uid, ids, field_name,arg,context=None):
       line_ids = []

       for rec in self.browse(cr, uid, ids, context=context):
            deposit_id = rec.id
            fdr_line_ids = self.pool.get('od.facility.management.fdr.line').search(cr,uid,[('deposit_id', '=', deposit_id)])
            if fdr_line_ids:
                for line in fdr_line_ids:
                    line_obj = self.pool.get('od.facility.management.fdr.line').browse(cr,uid,line,context=context)
                    management_id = line_obj.fdr_line_id and line_obj.fdr_line_id.id
                    allocated_amount = line_obj.allocated_amount
                    line_id = self.pool.get('od.deposit.management.line').create(cr,uid,{'management_id':management_id,'amount':allocated_amount})
                    line_ids.append(line_id)

       return dict([(id,line_ids) for id in ids])

    _columns = {
 #One2many              'management_line':fields.function(_get_management_line,type='one2many',relation="od.deposit.management.line"),
    }


Example3:


    def _get_lines_salary_rule_category(self, cr, uid, ids, field_names, arg=None, context=None):

        result = {}
        if not ids: return result
        for id in ids:
            result.setdefault(id, [])
        cr.execute('''SELECT pl.slip_id, pl.id FROM hr_payslip_line AS pl \
                    LEFT JOIN hr_salary_rule_category AS sh on (pl.category_id = sh.id) \
                    WHERE pl.slip_id in %s \
                    GROUP BY pl.slip_id, pl.sequence, pl.id ORDER BY pl.sequence''',(tuple(ids),))
        res = cr.fetchall()
        for r in res:
            result[r[0]].append(r[1])
        return result



    #One2many
    _columns = {
'details_by_salary_rule_category': fields.function(_get_lines_salary_rule_category, method=True, type='one2many', relation='hr.payslip.line', string='Details by Salary Rule Category'),
    }



Example4:




    def _get_latest_contract(self, cr, uid, ids, field_name, args, context=None):

        res = {}
        obj_contract = self.pool.get('hr.contract')
        for emp in self.browse(cr, uid, ids, context=context):
            contract_ids = obj_contract.search(cr, uid, [('employee_id','=',emp.id),], order='date_start', context=context)
            if contract_ids:
                res[emp.id] = contract_ids[-1:][0]
            else:
                res[emp.id] = False
        return res


        #many2one
        'contract_id': fields.function(_get_latest_contract, string='Contract', type='many2one', relation="hr.contract", help='Latest contract of the employee'),


Example5:


    def _sheet(self, cursor, user, ids, name, args, context=None):
        sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
        res = {}.fromkeys(ids, False)
        for ts_line in self.browse(cursor, user, ids, context=context):
            sheet_ids = sheet_obj.search(cursor, user,
                [('date_to', '>=', ts_line.date), ('date_from', '<=', ts_line.date),
                 ('employee_id.user_id', '=', ts_line.user_id.id),
                 ('state', 'in', ['draft', 'new'])],
                context=context)
            if sheet_ids:
            # [0] because only one sheet possible for an employee between 2 dates
                res[ts_line.id] = sheet_obj.name_get(cursor, user, sheet_ids, context=context)[0]
        return res



        'sheet_id': fields.function(_sheet, string='Sheet',
            type='many2one', relation='hr_timesheet_sheet.sheet',
         
            )



Many2many


'shift_ids':fields.many2many('od.time.slot','od_route_shift_detail_time_slot_relation',
'detail_id','time_slot_id','Shift'),



'promo_products': fields.many2many('product.product', 'callcenter_product_rel','product_id',

'callcenter_id', 'Call Center'),



Many2one



        'user_id': fields.many2one('res.users', 'Salesperson', states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, select=True, track_visibility='onchange'),



One2many


        'order_line': fields.one2many('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, copy=True),


Boolean Field


    'sent':fields.boolean(string='Sent')

Date field


    'date_invoice':fields.date(string='Invoice Date')

Text


        'note': fields.text('Terms and conditions'),

Char


        'name': fields.char('Order Reference', required=True, copy=False,
            readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, select=True),



        

No comments:

Post a Comment