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

I want to add a computed field to refer to the number of courses owned by each responsible

how can add this field and where ?

this is the code of Course class:

from odoo import api, fields, models,_


class AcademyCourse(models.Model):
_name="academy.course"
_description="Academy Course"

name=fields.Char(string="Course Name",required=True)
description=fields.Text(string="Course Description",required=True)
responsible_id=fields.Many2one('res.users',string="Responsible",ondelete="set null")
session_ids=fields.One2many('academy.session','course_id',string="Sessions")

_sql_constraints = {
('name_description_check',
'CHECK(name != description)',
'The title of the course should not be the description'),
('name_unique',
'UNIQUE(name)',
'The course title must be unique')
}




Ảnh đại diện
Huỷ bỏ
Tác giả

i don't have res.users class
how can i add this field inside it???

Solution 2:
Add One2many field with academy.course and relation field with responsible_id res.users after inherit in your code

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

from odoo import fields,models

class User(models.Model):    
_inherit="res.users"    

number_of_courses = fields.Integer(compute="_number_of_courses_count" , string="Number of courses")

@api.multi # remove this line if u are in odoo 14 or higher version

def _number_of_courses_count(self): 

      courses_line_ids = self.env['academy.course'].search([('responsible_id','=',self.id)])

     liste_of_all_courses = []

     for courses in courses_line_ids : 

            liste_of_all_courses.append(courses)

    self.number_of_courses = len(liste_of_all_courses)

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

to show the field in 'academy.course' views , use <responsible_id.number_of_courses>

PS : i did not test the code , please check the syntax.

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

Solution 1: 

Add Button box(smart button) in res.users using overriding view base.view_users_form with button type object

Write method in res.users with same name of button and open the Course using domain on users

Write method in users using inherit if you not have existing

class Users(models.Model):
_inherit = "res.users"

​def method_name(self):

​return view_code


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 8 25
3453
1
thg 7 25
1590
1
thg 8 25
1153
0
thg 5 25
1925
2
thg 4 25
4233