Skip to Content
Menu
This question has been flagged
12 Replies
17209 Views


 need to get the range between 2 years on a many2many field each year must be a record on the model

i added 2 fields to get the 2 years and and method to get the range between them

class yearrange(models.Model):
_name = 'yearrange'
_rec_name = 'name'

name = fields.Char()

 class autopart(models.Model):
_inherit = 'product.template'

@api.multi
@api.depends('start', 'end')
def years(self):
    record = [int(x) for x in range(int(self.start), int(self.end))]
    for rec in self:
        rec.rang=record

start = fields.Char(string="", required=False, )
end = fields.Char(string="", required=False, )
rang = fields.Many2many(comodel_name="yearrange", string="",compute=years )

i get records at the many2many field but all of them are = False 

Avatar
Discard

In this article I will show you how to create a many2many field in odoo. I will also show you guys how to filter many2many field using domain. You can also learn how to set default value on many2many field.

Reference: https://learnopenerp.blogspot.com/2018/12/add-domain-on-many2many-field-in-odoo.html

Author

i solve it by this code

class yearrange(models.Model):

_name = 'yearrange'

_rec_name = 'name'

name = fields.Char()

product_id = fields.Many2one(comodel_name="product.template")

@api.multi

@api.onchange('start', 'end')

def years(self):

print('innnnn')

for rec in self:

if rec.start and rec.end:

record = [int(x) for x in range(int(rec.start), int(rec.end) + 1)]

list = []

for item in record:

print(item)

range_id = self.env['yearrange'].create({

'name': str(item)

})

list.append(range_id.id)

rec.rang = [(4, x, None) for x in list]

start = fields.Char(string="", required=False, )

end = fields.Char(string="", required=False, )

rang = fields.One2many(comodel_name="yearrange", inverse_name="product_id", store=True, string="range")

Author Best Answer


i solve it by this code

class yearrange(models.Model):
_name = 'yearrange'
_rec_name = 'name'

name = fields.Char()
product_id = fields.Many2one(comodel_name="product.template")


class autopart(models.Model):
_inherit = 'product.template'

@api.multi
@api.onchange('start', 'end')
def years(self):
print('innnnn')
for rec in self:
if rec.start and rec.end:
record = [int(x) for x in range(int(rec.start)+1, int(rec.end)+1)]
list = []
for item in record:
print(item)
range_id = self.env['yearrange'].create({
'name': str(item)
})
list.append(range_id.id)
rec.rang = [(4, x, None) for x in list]

start = fields.Char(string="", required=False, )
end = fields.Char(string="", required=False, )
rang = fields.One2many(comodel_name="yearrange", inverse_name="product_id", store=True)



Avatar
Discard
Best Answer

Hello,

​rang is a Many2many field with model yearrange and it's functional field, so in computed method of that field you need to give ids  of yearrange model and you are assigning direct years.
So you can write as following :

@api.multi
@api.depends('start', 'end')
def years(self):
    record = [str(x) for x in range(int(self.start), int(self.end))]
    for rec in self:
        yearranges = self.env['yearrange'].search([('name','in',record)])
        rec.rang=yearranges

Avatar
Discard
Author

Thanks dear for your help i appreciate that too much but it's still not working for me

the field range doesn't updated and i added print (rec.rang) its print yearrange()

not any record

Best Answer


Hello,
In your case your  Many2many field range are used as functional filed(Compute field) So in this case you want to update your id in respective field.

So you following changes in your code maybe it's works for you

@api.multi
@api.depends('start', 'end')
def years(self):
    record = [str(x) for x in range(int(self.start), int(self.end))]
    for rec in self:
        yearranges = self.env['yearrange'].search([('name','in',record)]).ids
        OR
      yearranges = self.env['yearrange].search([('name','in',record)])
     rec.rang=yearranges

Thanks 

Avatar
Discard
Author

Thanks dear for your help i appreciate that too much but it's still not working for me

the field range doesn't updated and i added print (rec.rang) its print yearrange()

not any record

can you share the value of record?

Author

first solution rec and yearranges isn't used

if i added rec to rec.yearranges and print it display empty list []

second solution i get error

File "/odoo/odoo-server/odoo/osv/expression.py", line 318, in distribute_not

elif token in DOMAIN_OPERATORS_NEGATION:

TypeError: unhashable type: 'list'

ok, but what is the output of the record variable?

Author

it print list of range like >>>>>> ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019']

What is value in name ..? Similar like year ..?

Related Posts Replies Views Activity
2
Feb 20
10246
1
Jul 19
3418
4
Mar 15
31675
4
Feb 19
6623
1
Dec 17
5448