跳至内容
菜单
此问题已终结
3 回复
7006 查看

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"

形象
丢弃
编写者 最佳答案

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.

形象
丢弃
最佳答案

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,

形象
丢弃
编写者

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

相关帖文 回复 查看 活动
3
9月 24
46671
2
3月 15
6292
1
3月 15
7012
1
2月 17
5086
2
3月 22
2538