Skip to Content
Menu
This question has been flagged
2 Replies
1136 Views

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




Avatar
Discard
Author

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

Best Answer

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)

Avatar
Discard

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.

Best Answer

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


Avatar
Discard
Related Posts Replies Views Activity
2
Nov 24
275
1
Oct 24
336
4
Oct 24
327
2
Oct 24
364
2
Dec 24
681