1
votes

I had a model (res.partner) with a many2many field property (courses). It showed the courses a partner had joined to. In the kanban view of partners, I used the class oe_kanban_list_many2many, and I could see below the partners' name a list of tags indicating the courses the partner had joined to.

But, the situation changed. I needed to store some additional information about the relationship between partners and courses (joining_date), so I had to change the kind of the field courses (from many2many to one2many), and create a new intermediate table with the properties partner_id (many2one), course_id (many2one), and joining_date.

So, in summary:

BEFORE

res.partner

_columns = {
... 'courses': fields.many2many('course', 'res_partner_course_rel', 'partner_id', 'course_id', 'Courses'), ...
}

kanban view

<span class="oe_kanban_list_many2many">
    <field name="courses"/>
</span>

NOW

res.partner.course

_columns = {
    'partner_id': fields.many2one('res.partner', 'Partner'),
    'course_id': fields.many2one('course', 'Course'),
    'date': fields.date('Joining Date'),
}

res.partner

_columns = {
... 'courses': fields.one2many('res.partner.course', 'partner_id'), ...
}

kanban view

<span class="oe_kanban_list_one2many">
    <field name="courses"/>
</span>

In the kanban, I want to keep seeing on tags the courses a partner has joined to, but instead of that, I am seeing (n records). For example, if the partner 'John' has joined to two courses, I see, below 'John', (2 records).

How can I show the courses as before?

1

1 Answers

0
votes

Finally, I found a solution. May be is not the best, but works, and I hope it helps to anyone.

I declared a new field in res.partner, of type function:

'courses': fields.function(
            _get_courses,
            type='many2many',
            obj="course",
            method=True,
            string='Courses'),

Above this, I declared the function _get_courses, in which I make a SQL query to get all the courses a partner has joined. And the function returns a dictionary with this structure (let's show an example with ficticious data): {partner_id_1: [joined_course_id_1, joined_course_id_2,], partner_id_2: [joined_course_id_1,], partner_id_3: [joined_course_id_5, joined_course_id_8,],} ...

Then, in the XML kanban view, we show our new functional field inside the tags with class oe_kanban_list_many2many:

<span class="oe_kanban_list_many2many">
     <field name="courses"/>
</span>

And that's it, now I can see into tags every course a partner has joined.