Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to override create method

    • 0
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 3.20k
    Answer it

    Create:

    - Create method use for creating new record with given values of the record.

    - Create method return created newly record with given values.

    Syntax:

    create(cr, uid, values, context=None)

    Ex: Here method override for version 7.0:-

    def create(self, cr, uid, vals, context=context):
    
          if not vals:
    
               vals = {}
    
          if vals:
    
                vals = {'name': 'Shreyash Patel', 'age': 22}
    
          super(res_partner, self).create(cr, uid, vals, context=context)

    For version 8.0 with api:-

    @api.model
    
    def create(self):
    
          if not vals:
    
               vals = {}
    
          if vals:
    
                vals = {'name': 'Shreyash Patel', 'age': 22}
    
          super(ResPartner, self).create(cr, uid, vals, context=context)

     

 4 Answer(s)

  • Hi all,
              In overriding create/write method you should be carefull !!
    lets take an example,

    def create(self,cr,uid,vals,context):
          res=super(class_name,self).create(cr,uid,vals)
          # do anything with your logic #
           return res

    So, While overriding the orm method you should first call the super class create method and and apply your logic and after that just return that res variable
             
  • syntex:

    @api.model

    def create(self, vals):

       # do something here
    
        return super(class_name, self).create(vals)
    

    example: if you want to override the sale object create method, do something like code into the file

    @api.model

    def create(self, vals):

     # do something 
    
     record = super(SaleOrder, self).create(vals)
    
     # do something, if you want to do otherwise return 
    
     return record
    

    check here more about override the method live example:

    https://github.com/odoo/odoo/blob/master/addons/account/models/account.py#L404

    1. def create(self, cr, uid, vals, context=None): -->Method Returns to Super Method

      vals['allday']=False
      eventid=super(modelname, self).create(cr, uid, vals, context=context)
      meetingobj=self.browse(cr,uid,eventid,context=context)
      Your code Here..................
      
      
      return eventid
      
    2. def create(self, cr, uid, vals, context=None): -->Method Returns to Super Method vals['all_day'] = False Your code Here.................... return super(modelname, self).create(cr, uid, vals, context=context)

    3. If you want to Omit your base create method and need to create your own method means , Use following code.This method is not return to Super create method.

      def create(self, cr, uid, vals, context=None): vals['all_day'] = False Your code Here.................... res = osv.osv.write(self, cr, uid, ids, values, context=context) return res

  • class res_users(models.Model): _inherit ='res.users'

    @api.model
    def create(self, vals):
    
        #Write your logic here
        print 'Fields and their values to be created in a dictionary',vals
        res = super(res_users, self).create(vals)
    
        #Write your logic here
    
        return res
    

    full details here: http://odootechnical.com/learn-overriding-create-method-in-odoo-8/

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: