This question has been flagged
1 Reply
2791 Views

i want to show for every value of a selection field a certain forme of sequence 

here is my code 

def generate_budget_no(self):
        if self.sequence == 'New':
            if self.type_bdg == 'hit':
                self.sequence = self.env['ir.sequence'].next_by_code('seq.hit')
            if self.type_bdg == 'dun':
                self.sequence = self.env['ir.sequence'].next_by_code('seq.dun')
            elif self.type_bdg == 'nv':
                self.sequence = self.env['ir.sequence'].next_by_code('seq.nv')
            return True


i have checked that sequences records are declared in the _manifest.py

but even when i click the button, it dosent generate the required sequence


Avatar
Discard

You have to call this in create method and use vals parameter to check the value of the type_bdg and sequence because the self not have the value of the field yet.

def create(self,vals):

if vals.get('name', 'New') == 'New':

type_bdg = vals.get('type_bdg', False)

if type_bdg == 'hit':

vals['sequence'] = self.env['ir.sequence'].next_by_code('seq.hit')

if type_bdg == 'dun':

vals['sequence'] = self.env['ir.sequence'].next_by_code('seq.dun')

elif type_bdg == 'nv':

vals['sequence'] = self.env['ir.sequence'].next_by_code('seq.nv')

return super([YourModelName], self).create(vals)

Hello,

I would suggest logging the variables "self.sequence" and "self.type_bdg", to make sure the values are what you are expecting.

Could you supply extra code linking to, what I assume is a selection field, char field and where you are calling the generate_budget_no function from.

Thanks,

Author

class BudgetBudget(models.Model):

_inherit = 'budget.budget'

sequence = fields.Char(string='Sequence', readonly="1")

type_bdg = fields.Selection([('hit', 'HIT'), ('dun', 'Dun'), ('nv', 'NV')], default='nv', string='Type')

def create(self,vals):

if vals.get('sequence', 'New') == 'New':

type_bdg = vals.get('type_bdg', False)

if type_bdg == 'hit':

vals['sequence'] = self.env['ir.sequence'].next_by_code('seq.hit')

if type_bdg == 'dun':

vals['sequence'] = self.env['ir.sequence'].next_by_code('seq.dun')

elif type_bdg == 'nv':

vals['sequence'] = self.env['ir.sequence'].next_by_code('seq.nv')

return super(BudgetBudget, self).create(vals)

<record id="seq_hit_id" model="ir.sequence">

<field name="name">BSH</field>

<field name="code">seq.hit</field>

<field name="prefix">BSH</field>

<field name="padding">5</field>

</record>

<record id="seq_dun_id" model="ir.sequence">

<field name="name">BSD</field>

<field name="code">seq.dun</field>

<field name="prefix">BSD</field>

<field name="padding">5</field>

</record>

<record id="seq_nv_id" model="ir.sequence">

<field name="name">BSN</field>

<field name="code">seq.nv</field>

<field name="prefix">BSN</field>

<field name="padding">5</field>

</record>

here is my code

Author Best Answer

i just tried your code, but still got nothing, 

i've also correct the first if statment, sequence istead of name 

 if vals.get('sequence', 'New') == 'New':


Avatar
Discard