Skip to Content
Menu
This question has been flagged
1 Reply
4779 Views

I've simplified my code for the sake of this question, but basically I have this  

class Booking(models.Model):
_name = 'ff.booking'
boat_ride_bookings = fields.Many2many('ff.boat_ride_booking')
foo = fields.Integer()

class BoatRideBooking(models.Model):
_name = 'ff.boat_ride_booking'
booking = fields.Many2many('ff.booking')
foo = fields.Integer()

In another place (outside of both of these models) I'm trying to do create some BoatRideBooking objects on existing Booking objects and then connecting them.

This works

# booking is a referance to an existing record
brb = self.env['ff.boat_ride_booking'].create({ 'foo': 1 })
booking.boat_ride_bookings = [(4,brb.id)]

But I want to do something like this

# booking is a referance to an existing record
brb = self.env['ff.boat_ride_booking'].create({ 'foo': 1, 'booking': booking.id })

or even better

# booking is a referance to an existing record
booking.boat_ride_bookings = [(0,0,{ 'foo': 1, 'booking': booking.id }]

but that neither works. I probably don't have the right syntax and I've been looking everywhere and trying a lot.

Someone know the right syntax on how to solve this problem?

Thanks!

Avatar
Discard
Best Answer

Hi Alf,

In your case you can define declare fields with same table name like following:

class Booking(models.Model):
_name = 'ff.booking'
boat_ride_bookings = fields.Many2many('ff.boat_ride_booking','ff_boat_booking_rel', 'ff_id', 'boat_ride_id', string="field name")
class BoatRideBooking(models.Model):
_name = 'ff.boat_ride_booking'
booking = fields.Many2many('ff.booking','ff_boat_booking_rel', 'boat_ride_id', 'ff_id', string="field name")

Both object Many2many belongs to same table with same column name, just need change the column name places based on objects, and if you create either of any one many2many  other object many2many would connect by same time.


Avatar
Discard
Related Posts Replies Views Activity
1
Feb 24
839
0
Jul 24
1503
0
Jul 22
60
1
Jul 22
1409
2
Dec 23
31969