Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odpovědi
7235 Zobrazení

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
Zrušit
Autor Nejlepší odpověď

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
Zrušit
Nejlepší odpověď

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
Zrušit
Autor

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 Odpovědi Zobrazení Aktivita
3
zář 24
47030
2
bře 15
6496
1
bře 15
7267
1
úno 17
5277
2
bře 22
2798