跳至內容
選單
此問題已被標幟
2 回覆
1890 瀏覽次數

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')
}




頭像
捨棄
作者

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

最佳答案

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)

頭像
捨棄

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.

最佳答案

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


頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
2
8月 25
3463
1
7月 25
1599
1
8月 25
1153
0
5月 25
1930
2
4月 25
4247