Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
1886 Widoki

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




Awatar
Odrzuć
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

Najlepsza odpowiedź

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)

Awatar
Odrzuć

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.

Najlepsza odpowiedź

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


Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
sie 25
3454
1
lip 25
1594
1
sie 25
1153
0
maj 25
1927
2
kwi 25
4235