Skip to Content
Menu
This question has been flagged
3 Replies
9737 Views

Hi,

I have Students Model and I'm trying to set  the field subscr_code depending on the field Category :

  • If Category is 'E' : subscr_code would be in [ 0001 -> 9999]

  • If Category is 'D' : subscr_code would be in [0001 -> 9999]

  • If Category is 'C' : subscr_code would be in [0001 -> 9999]

I want to query the max value of subscr_code where the Category == 'E' (for example) and then increment this value and set it in the new record.

How can I do this ? Can anyone help me please ?




Avatar
Discard
Best Answer

For this purposes, Odoo prepares a special tool - 'ir.sequence'

You should define such a sequence for each category, e.g.:

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

            <field name="name">Category E</field>

            <field name="padding" eval="0"/>

            <field name="code">E Category Student</field>

            <field name='company_id' eval='1'/>

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

        </record>

Then, in 'create' of your function you should check a category, and apply a number. How to do so: just look at the module 'sale' > the function 'create' of a sales order.


As for the max possible value: you may just apply sql or api contraint. But in such a way you should understand: that not more than 10000 students for each caregory may be generated.

Avatar
Discard

To have zeros in front ('0010' instead of just '10') use the field 'padding' of 'ir.sequence'. You may also have a special prefix: e.g. prefix 'E-' would lead to 'E-0010'

Author Best Answer

Thanks IT Libertas, your comment is very useful.
I did this work :

1- add a new file ir_sequence_data.xml under data folder. Its content is in the following link:
https://codepaste.net/bzqryw I got errors messages when I definded a sequence type for each sequence. So I commented all sequence types.

2- Then I added this file to __manifest__.py. I got this log when I update the app :
2017-04-19 21:25:59,763 7266 INFO Malik-DEV odoo.modules.loading: loading openeducat_core/data/ir_sequence_data.xml 3- I add this code to Student.py model :
subscr_code = fields.Integer("Subscription code", readonly=True)
...
    @api.model
    def create(self, vals):
        print "+++++++++++++++++++++++++++++++++++++++++++++++++"
        #print vals['category_id'].code
        if vals['category_id'].code == 'C':
            vals['subscr_code'] = self.env['ir.sequence'].next_by_code('C')
        elif vals['category_id'].code == 'D':
            vals['subscr_code'] = self.env['ir.sequence'].next_by_code('D')
        elif vals['category_id'].code == 'E':
            vals['subscr_code'] = self.env['ir.sequence'].next_by_code('E')
        #print vals['subscr_code']
        print "+++++++++++++++++++++++++++++++++++++++++++++++++"
        return super(OpStudent, self).create(vals)


4- But When I add a new student and click "Create", I takes a long time loading infinitely.
And It shows these log messages :
https://codepaste.net/fp85fe

Have you any idea about the problem here? Am I missing something ?

What does _('New') mean in the create() function of sale model ?

Avatar
Discard
Author

I found it, the problem was with vals['category_id'].code :

vals['category_id'] is the id of category, not an object. It can't access the field "code" like this. The solution is :

@api.model

def create(self, vals):

print "+++++++++++++++++++++++++++++++++++++++++++++++++"

Category = self.env['op.category']

newCategory = Category.browse(vals['category_id'])

code = newCategory.code

print code

if code == 'C':

vals['subscr_code'] = self.env['ir.sequence'].next_by_code('C') or 111

print "C case !!!"

elif code == 'D':

vals['subscr_code'] = self.env['ir.sequence'].next_by_code('D') or 222

print "D case !!!"

elif code == 'E':

vals['subscr_code'] = self.env['ir.sequence'].next_by_code('E') or 333

print "E case !!!"

else:

print "nothing to do !!!"

print "+++++++++++++++++++++++++++++++++++++++++++++++++"

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