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


  How do I automatically retrieve the data from a field which is already stored in the database so that it can be used the autofill another many2one field which is new ? for example

I have a course_id field which already has Its Stored data in the database

course_id = fields.Many2one ('op.course', 'Course') 

Then i created another new program_id field inside the same class

program_id = fields.Many2one ('op.program', 'Program', domain = "[('course_id', '=', course_id)]")

 When i tried the onchange method, it works perfectly

@ api.onchange ('course_id') 
        def onchange_course (self): 
                    prog_id = false 
                   if self.course_id and self.course_id.program_id: 
                    prog_id = self.course_id.program_id.id 
                    self.program_id = prog_id 

But now i want program_id field to be updated automatically in the tree and form view  by using the course ids which are already stored. Am using Odoo version 12


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

Hi! One way also is to use compute attribute in your fields declaration.

E.g.: 

program_id = fields.Many2one ('op.program', string='Program', domain = "[('course_id', '=', course_id)]"
            compute="_your_method_name")
@ api.depends('course_id') 
def _your_method_name(self): 
    for record in self:                                                         
        prog_id = false 
        if self.course_id and self.course_id.program_id: # just do your work around starting on these lines and you'll get want you want in no time 
           prog_id = record.course_id.program_id.id #map your values correctly here
           record.program_id = prog_id 

This will automatically update both your form and tree view for the referred field.

Hope it helps.

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

Hello Brian,

You can also use the related field to get the value of program_id automatically.

try this code,

program_id = fields.Many2one ('op.program', related='course_id.program_id', string='Program', domain = "[('course_id', '=', course_id)]")


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 11 23
1809
1
thg 5 18
5212
1
thg 8 15
3526
2
thg 12 24
6363
selenium dont work Đã xử lý
1
thg 5 24
2999