Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
2 Antworten
1873 Ansichten

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
Verwerfen
Autor

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

Beste Antwort

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
Verwerfen

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.

Beste Antwort

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
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
2
Aug. 25
3454
1
Juli 25
1594
1
Aug. 25
1153
0
Mai 25
1927
2
Apr. 25
4235