Skip to Content
Menu
This question has been flagged
4 Replies
1799 Views

Hello all,

i am new in Odoo and i try my first steps to create a sequece and a button to increment it on button click.

I created my own module and added my files.

Here my files:

debit_card.py

from odoo import models, fields, api, _

class DebitCard(models.Model):
_name="debit.card"
_description="Debit Card"

debit_seq = fields.Char(
string="Debit Card Datev Sequence",
required=True,
copy=False,
readonly=True,
index=True,
default=lambda self: _("New"))

@api.model
def create(self, vals):
if vals.get('debit_seq', _('New')) == _('New'):
vals['debit_seq'] = self.env['ir.sequence'].next_by_code('debit.card.sequence.code') or _('New')
result = super(DebitCard, self).create(vals)
return result

debit_card_view.xml (can´t post correct code. don´t know why...)


        (record id="debit_card_form_view" model="ir.ui.view")
            (field name="name")debit.card.form.view(/field)
            (field name="model")debit.card(/field)
            (field name="arch")type="xml"(/field)
             (form string="Debit Card")
                    (div)Hello World! (/div)
                    (field name="debit_seq" style="font-size:22px;font-weight:bold;"/)
             (/form)
          (/field)
        (/record)
       
   

sequence.xml

(odoo)
(data noupdate="1")
(!-- Sequence for debit.card //--)
(record id="debit_card_datev_sequence" model="ir.sequence")
(field name="name")Debit Card Datev Sequence(/field)
(field name="code")debit.card.sequence.code(/field)
(field name="padding")7(/field)
(field name="number_next")62071(/field)
(field name="number_increment")1(/field)
(field name="company_id" eval="False"/)
(/record)
(/data)
(/odoo)


Now, if i call the debit card it shows me: "Hello World!"

But no sequence but "New"


I found many "examples" on web search but nothing helps me.


Where is the issue for not showing the sequence?

And how to implement a button to increment the sequence on click?

Avatar
Discard
Best Answer

Hi,

you can create a button like

def seq_increment(self):
​for rec in self:
​rec.debit_seq = self.env['ir.sequence'].next_by_code('debit.card.sequence.code')

and xml would like

(record id="debit_card_form_view" model="ir.ui.view")
            (field name="name")debit.card.form.view(/field)
            (field name="model")debit.card(/field)
            (field name="arch")type="xml"(/field)
             (form string="Debit Card")
                    (header)
​(button name="seq_increment" type="object" string="Increment" class="oe_highlight" id="button_seq_increment"/)
​(/header)
             (/form)
          (/field)
        (/record)


thanks

Avatar
Discard
Author

Thank you very much. It works fine but it doesn`t use the next_number. I changed the field name next_number to next_number_actual because in Settings-Technical-Sequences the field name for the next number is: next_number_actual.
And if i call the "page" debit card first, it always shows "New". Is this because in the field declaration is the default value lambda self: _("New")? If yes, how may i show the current value of the sequence?

Best Answer

Hi,
If you already have a sequence record and if you need to increment the sequence value on a button click, inside the button function, you can add below line of code to get next number from the sequence.

Code: self.env['ir.sequence'].next_by_code('code_Value_of_Sequence')


Thanks

Avatar
Discard
Author Best Answer

The titel changed the value after clicking on the "increment button" and i don`t know why.

Can anyone explain me this behavior?

befor click:

after clicked.


Avatar
Discard

by default, odoo is taking field 'name' as the record name, if your model doesnt have a field called 'name' it will shows as model_name,record_id, in your case it is debit.card,11.

if you need to show any other field as the record name, kindly provide that field in _rec_name after declaring the model name.

ie;
_name = 'debit.card'
_description = 'Debit Card Details'
_rec_name = 'seq_increment'

this will solve your issue.

thanks

Best Answer

Hi,


You should declare your sequence field in XML view to display it.

Like this, <field name="name"/>



Hope it helps

Avatar
Discard
Author

The sequence field is declared in the xml view but it displayed always "New".
I need help for adding a button to increment the sequence and update the field output.

Related Posts Replies Views Activity
0
Feb 23
4765
1
Jan 24
14165
0
Jan 23
94
1
Oct 22
4125
0
Apr 22
2693