Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odgovori
7257 Prikazi

How can I have the functionality to have related objects from the same model? i.e. I have a custom module for Songs Catalogue. I want to be able to add others related Songs (link them together) so when I open a Song in form view I can see "Similar/Related Songs"

Avatar
Opusti
Avtor Best Answer

I have done it like that:

class MusicSong(models.Model):
_name = 'music.song'

    related_ids = fields.Many2many('music.song','music_song_related','music_song_ids','music_song_related_ids')
    related_reverse_ids = fields.Many2many('music.song','music_song_related','music_song_related_ids','music_song_ids')
    related_songs_computed = fields.Many2many('music.song','music_song_related',string="Related Songs",compute='_compute_related_songs', inverse='_inverse_related_songs')

    def _compute_related_songs(self):
        song_ids = self.related_ids.ids + self.related_reverse_ids.ids
        for song in self:
            song.related_songs_computed = song_ids

     def _inverse_related_songs(self):
        for song in self:
            song.related_ids = [(6,0,song.related_songs_computed.ids)]

It works but I'm sure there is more correct way of doing this. I would appreciate any input.

Avatar
Opusti
Best Answer

Hello Mageno,

In odoo, you can find same functionality under Customer(res.partner).

Here Company type partner have more then one Child.

You can get some help from there.

Hope this will help you,

Avatar
Opusti
Avtor

Hmmm not really,

you see in the res.partner example you see all the partners with common parent but I need a cross reference. In product A you see product B as related and in B you see A. And beside this I would like it to be many2many relation

Related Posts Odgovori Prikazi Aktivnost
3
sep. 24
47098
2
mar. 15
6517
1
mar. 15
7280
1
feb. 17
5304
2
mar. 22
2837