Skip to Content
Menu
This question has been flagged
3 Replies
5789 Views

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
Discard
Author 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
Discard
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
Discard
Author

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 Replies Views Activity
3
Sep 24
43963
2
Mar 15
5498
1
Mar 15
6106
1
Feb 17
3928
2
Mar 22
1269