Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
5287 Lượt xem

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!

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 2 24
1867
0
thg 7 24
2828
0
thg 7 22
60
1
thg 7 22
2758
2
thg 12 23
33302