Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
6 Trả lời
12580 Lượt xem

i want to change the value of the field (name) of the model(categ.age) based on the value of the field (age) that belongs to the modele(mod.age) 

i declared a One2many relation between the models like the code bellow and i used a related field but the value still not take in consideration the changing value 

                class age(models.Model):

                _name = "mod.age"

                category = fields.Many2one('categ.age', 'group of age')

                name_test = fields.Char(compute="set_age_group", related='category.name')

                age = fields.Integer('Age')


              @api.depends('age')

              def set_age_group(self):

                    if self.age < 20:

                        self.name_test = 'type2'

                   else:

                       self.name_test = 'type3'


             class cat_age(models.Model):

             _name = "categ.age"

             _description = "group of age"

            name = fields.Char(string="Name of categorie", store=True, readonly=False)

            other_field = fields.Char(string="other_field")



Ảnh đại diện
Huỷ bỏ

Hope this will helps you in future: http://learnopenerp.blogspot.com/

Tác giả

hello sehrish, are you puting the wrong link ? cuz i don't see in which that could be helpfull

Tác giả

Thanks Alejandro Aladro, but that still not resolve the probleme

Câu trả lời hay nhất

Hi!

You can do like the following code, it's just a suggestion, you can adapt it as you need:

class Age(models.Model):
    _name = "mod.age"
    _rec_name = "age"
   
    age = fields.Integer('Age')
    category_id = fields.Many2one('categ.age', 'Group of age')
   
    @api.onchange('age')
    def onchange_age(self):
        self.category_id.name = 'type2' if self.age < 20 else 'type3'

class Category(models.Model):
    _name = "categ.age"
    _description = "Group of age"
   
    name = fields.Char("Name")

Best regards!

Ảnh đại diện
Huỷ bỏ

Can you point this to computing a fields value?

Câu trả lời hay nhất

Hello,in your case the name_test is wrong defined.

you need remove attribute related for the computed method work.

related method is a computed method of odoo...

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thanks @Sylvain for replay, i tried the code and didn't work (the field still not changed) i think the code that you are suggesting will work if the name field be a selection field 

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 1 21
3462
1
thg 12 20
3820
2
thg 10 23
2292
1
thg 5 21
4686
1
thg 1 21
5281