Ridma Gimhani
Ridma Gimhani

Reputation: 133

Looking for a best way to insert a records from one model to another based on selection in odoo

I did the code for insert records from so_parts table to so_bo table using Query...How can I use ORM method to do this kind of job. Is there any other way(best)to do that? Here is my code`

`

@api.multi
    def save_rapair_parts(self, vals):
        #get todays date and convert it to string
        created_date    = datetime.datetime.today().strftime("%m/%d/%Y")
        str_date        = str(created_date)
        so_p_id         = self.so_p_id.id
        bo_status       = self.bo_status
        so_part_t       = self.so_part_t
        so_part_sno     = self.so_part_sno
        product         = self.so_part_product
        product_str     = 'Repair '+str(product)
        part_id         = self.id
        bench_order_table.search(['id','bo_sno','created_date','bo_number','rep_description','bo_status'])


    #insert details intoso bench orders
`
        if so_part_t=='r_b':
            try:
                sequence     = self.env['ir.sequence'].next_by_code('so.benchorder') or '/'
                str_sequence = str(sequence)

                query = """SELECT so_work_authorization FROM my_depots_so WHERE id=%d """ % (so_p_id)
                self.env.cr.execute(query)
                result = self.env.cr.fetchall()
                result_number = json.dumps(result, ensure_ascii=False)
                strip_number = result_number.strip('\' \" [] ')
                work_auth_no = str(strip_number)
                work_auth_no += "-"
                work_auth_no += str_sequence
                insert ="""INSERT INTO my_depots_so_bo(id,so_bo_id,bo_sno,created_date,bo_number,rep_description,bo_status) values %s """
                parameters = (part_id,so_p_id,so_part_sno,str_date,work_auth_no,product_str,bo_status)
                self.env.cr.execute(insert,(parameters,))
                my_depots_bo(id,bo_sno,created_date,bo_number,rep_description,bo_status) values %s """
                #   self.env.cr.execute(insert_query, (parameters,))
            except Exception:
                print "Error in inserting values"`

Upvotes: 2

Views: 235

Answers (1)

Charif DZ
Charif DZ

Reputation: 14721

yes there is a better way because when you use ORM method you also checks access right for user to:

for your select query:

rec = self.env['my.depots.so'].search_read(['id', '=', so_p_id], ['so_work_authorization'])
if rec:
    rec = rec[0] # search_read return a list of dictionary
    so_work_authorization = rec['so_work_authorization']
    # and do what ever you want with the result

    # to create 
    # call create method witch accept a dictionary 
    # field_name : value
    new_rec = self.env['my.depots.so.bo'].create({
        'so_bo_id': so_p_id, # many2one must be an integer value 
        'bo_sno': bo_nso_value,
        'bo_number': value_of_number,
        # ....
        # ....
        # add al field
    }) # create return the new created record as model object

for inserting use: self.env['model.name'].create(vals) for updating use : self.env['model.name'].write(vals)

using ORM method makes sure that user don't pass the security access rigths Hope you get the idea

Upvotes: 1

Related Questions