Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to generate automatic sequence number in one2many field?

By
rosey
on 11/12/14, 7:42 AM 1,997 views

I want to generate sequence number in purchase.requisition.line. Below is my code. Can anyone please correct me?

class purchase_requisition(osv.osv):
      _inherit = 'purchase.requisition'
      
      def update_seq(self, cr, uid, ids, line_ids, context=None):
          print "line_ids",line_ids
          new_order_line = []
          counter = 1
          x = {}
          for line in line_ids:
              if line[0] in [1,4]:
                 line[0] = 1
              if type(line[2]) == type({}):
                 line[2].update({'serial_no':counter})
                 print "counter1",counter
                 #x = counter
              else:
                 line[2] = {'serial_no':counter}
                 print "counter2",counter
                 #x = counter
              counter = counter + 1
              print "counter",counter
              x = new_order_line.append(line)
              print "new_order_line",new_order_line
          return {'value': {'line_ids': x} }
      
      


class purchase_requisition_line(osv.osv):
      _inherit = 'purchase.requisition.line'
      
      def update_seq(self, cr, uid, ids, line_ids, context=None):
          print "line_ids",line_ids
          new_order_line = []
          counter = 1
          for line in line_ids:
              if line[0] in [1,4]:
                 line[0] = 1
              if type(line[2]) == type({}):
                 line[2].update({'serial_no':counter})
                 print "counter1",counter
              else:
                 line[2] = {'serial_no':counter}
                 print "counter2",counter
              counter = counter + 1
              print "counter",counter
              new_order_line.append(line)
              print "new_order_line",new_order_line
          return {'value': {'serial_no': new_order_line} }


      _columns={
            'serial_no':fields.integer('Serial No'),
      }
      
      _order = 'serial_no desc, serial_no, id'
      _defaults = {'serial_no': 1,
                 }

purchase_requisition_line()

 

I didnt get the output.

 

i didnt get step 1 and step 2. can you please explain?

rosey
on 11/13/14, 2:03 AM

Its already explained. you need to pass onem2any field in context. for example your one2many field name is line_ids than Than override the name_get method i mean use the code which i have posted to your one2many model. it will work for your sequence.

Anil R. Kesariya
on 11/13/14, 2:06 AM

i tried but its not working. i will try again

rosey
on 11/13/14, 2:07 AM
2

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 11/13/14, 1:52 AM

Hello Rosey,

Here I wrote simple code to generate next sequence for one2many field.

step1:   pass your one2many field it self in property context from xml.

             <field name="ref_ids" context="{'ref_ids':ref_ids}"/>

step2:  override the name_get method of one2many field model

    def default_get(self, cr, uid, ids, context=None):
        res = {}
        if context:
            context_keys = context.keys()
            next_sequence = 1
            if 'ref_ids' in context_keys:
                if len(context.get('ref_ids')) > 0:
                    next_sequence = len(context.get('ref_ids')) + 1
        res.update({'sequence': next_sequence})
        return res

Note :consider line)ids is your one2many field name in your model you need to pass and use whatever your field you have used and sequence is field name for sequence in one2many fields' model. in your case might be different.

Hope this will useful for you.

Thank you.

 

Its already explained. you need to pass onem2any field in context. for example your one2many field name is line_ids than Than override the name_get method i mean use the code which i have posted to your one2many model. it will work for your sequence.

Anil R. Kesariya
on 11/13/14, 2:06 AM

i have a doubt, when will the function call?

rosey
on 11/13/14, 2:11 AM

its default_get function you don't need to call it will automatically called by openerp framework when you click on add item button it will be call automatically.

Anil R. Kesariya
on 11/13/14, 2:13 AM

if 'line_ids' in context_keys: what the if condition means?

rosey
on 11/13/14, 2:22 AM

if 'line_ids' in context_keys: what the if condition means? thanks for the quick response

rosey
on 11/13/14, 2:23 AM

function is not working from if 'line_ids' in context_keys: what i need to check. please help me...

rosey
on 11/13/14, 2:34 AM

I have improved code now check it. I have used line_ids. but instead line_ids you need to use your field. replace line_ids with your one2many field.

Anil R. Kesariya
on 11/13/14, 2:40 AM

one last question: what is ref_ids? is it my field?

rosey
on 11/13/14, 2:55 AM

thanks its working

rosey
on 11/13/14, 3:26 AM

@Anil, I tried your code. But there is a problem, if a record is deleted. For instance, 1,2,3,4,5.. I deleted sequence=4. then according to your code, the next sequence will be len(list)=4 so 4+1==> 5. How to solve this?

Atchuthan - Technical Consultant, Sodexis Inc
on 11/14/14, 12:48 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 11/12/14, 7:42 AM
Seen: 1997 times
Last updated: 3/16/15, 8:10 AM