如何在odoo中按顺序添加动态前缀

时间:2018-06-21 11:37:27

标签: odoo odoo-11

我正在尝试使用动态前缀来实现序列,但是我不能。 自* .py以来,我无法将变量传递给我的* .xml 我尝试使用“上下文”和“域”,但收到以下错误消息: “ AssertionError:元素odoo具有额外的内容:记录,第4行”

我的xml代码是:

<?xml version="1.0" encoding="utf-8"?>
<odoo>

   <record id="seq_obra" model="ir.sequence">
      <field name="name">Secuencia Obra</field>
      <field name="code">secuencia.obra</field>
      <field name="prefix">%(prefijo)</field>  <-- this is the variable that i want to read from my .py
      <field name="padding">3</field>
   </record>

我的py代码是:

class Poscontrata(models.Model):
   _name = 'poscontrata'
   name = fields.Char(string='Descripcion', required=True)
   sigla = fields.Char(required=True)
   area_id = fields.Many2many('area', string='Area')
   active = fields.Boolean(default=True)

   @api.model
   def create (self,values):
      area_cod =  values['area_id'][0][2]
      ctd_area_dsc = len(area_cod)

      for x in range(0, ctd_area_dsc):
      prefijo = '-' + values['sigla'] + '-' + self.env['area'].search([('id', '=', area_cod[x])], limit=1).sigla + '-'   
      function_call_sequence_and_send_variable('secuencia.obra','prefijo') <-- this is the function that i need

      return super (Poscontrata,self).create(values)

预先感谢

2 个答案:

答案 0 :(得分:1)

我在python函数中求解了,只取序列的下一个数字,然后构造我的新名称。

.py中的Python函数:

@api.model
    def create(self, vals):
        correlativo_en_db = self.env['ir.sequence'].next_by_code('ruta.sequence')
        numero_correlativo = re.findall("\d+", correlativo_en_db)[0]
        fecha_entrega = vals['fecha']
        correlativo = fecha_entrega + '/' + numero_correlativo
        vals['name'] = correlativo
        result = super(Ruta, self).create(vals)

我的顺序记录(XML):

    <record id="seq_ruta" model="ir.sequence">
        <field name="name">Ruta</field>
        <field name="code">ruta.sequence</field>
        <field name="prefix">X</field>
        <field name="padding">2</field>
        <field name="use_date_range">True</field>
        <field name="company_id" eval="False"/>
    </record>

答案 1 :(得分:0)

尝试一下:

name = self.env ['ir.sequence']。get('your_sequence_code')