Reputation: 123
I want to add a sequence field "name"
that have this syntax : "Alerte/date_creation/000x"
it will be like :
"Alerte/14-07-2021/0001"
"Alerte/14-07-2021/0002"...
here my python code :
class StockAlert(models.Model):
_name = "stock.alert"
_inherit = ['mail.thread', 'mail.activity.mixin']
_rec_name = "name"
name = fields.Char(string="Nom",default="New")
date_creation = fields.Date("Date de création",default=datetime.now().strftime('%Y-%m-%d'))```
Upvotes: 1
Views: 1265
Reputation: 1155
Two possibilities managed yourself via your own code (Bad idea) or used ir.sequence.
In the 2 cases, you will use method create
Managed yourself
@api.model
def create(self, values):
res= super(YourClass, self).create(values)
res.write({name : f'Alerte/{current_date}/{res.id}'})
return res
This logic worked but you used 2 requests to doing this 1 Insert and 1 update.
Use ir.sequence
First in xml file you create a ir.sequence:
<record id="seq_stock_alert" model="ir.sequence">
<field name="name">Stock alert sequence</field>
<field name="code">seq.stock.alert</field>
<field name="prefix">Alerte/%(y)s-%(month)s-%(day)s/</field>
<field name="padding">3</field>
</record>
Next in your python
@api.model
def create(self, values):
values['name'] = self.env['ir.sequence'].next_by_code('seq.stock.alert') or _('New')
return super(YourClass, self).create(values)
Upvotes: 3