Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
15 Trả lời
13717 Lượt xem

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.

 

Ảnh đại diện
Huỷ bỏ

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

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.

Tác giả

i tried but its not working. i will try again

Câu trả lời hay nhất

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.

 

Ảnh đại diện
Huỷ bỏ

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.

Tác giả

i have a doubt, when will the function call?

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.

Tác giả

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

Tác giả

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

Tác giả

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

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.

Tác giả

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

Tác giả

thanks its working

@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?