Help

0

Field referring to the same model?

Avatar
Magen

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
2 Answers
0
Avatar
Magen
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
0
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,

1 Comment
Avatar
Discard
Avatar
Magen
-

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