1π
β
At the moment when you save the model for the first time, the ManyToManyField
will never contain any values. Indeed, Django first needs to save the record in the database before it can add items in the many-to-many relation, since it needs the primary key of the ReservationOrder
.
You can use the m2m_changed
signal [Django-doc] which will fire if you add/remove/clear items from the many-to-many relation before or after these are saved in the database.
But signals are often considered an anti-pattern, since it makes it less predictable what .save()
will do, in what order, etc. Usually it is better to encapsulate the logic in a function, and call that function in the views that will create/update the Reservationorder
.
Source:stackexchange.com