Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
12 Replies
18850 Tampilan


 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
Buang

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

Penulis

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")

Penulis Jawaban Terbai


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
Buang
Jawaban Terbai

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
Buang
Penulis

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

Jawaban Terbai


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
Buang
Penulis

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?

Penulis

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?

Penulis

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

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

Post Terkait Replies Tampilan Aktivitas
2
Feb 20
12375
1
Jul 19
4568
4
Mar 15
33243
4
Feb 19
7749
1
Des 17
6553