콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
12 답글
18885 화면


 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 

아바타
취소

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

작성자

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

작성자 베스트 답변


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)



아바타
취소
베스트 답변

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

아바타
취소
작성자

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

베스트 답변


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 

아바타
취소
작성자

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?

작성자

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?

작성자

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

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

관련 게시물 답글 화면 활동
2
2월 20
12402
1
7월 19
4591
4
3월 15
33252
4
2월 19
7754
1
12월 17
6569