[Answer]-Update existing M2M relationship in Django

1👍

You are misunderstanding what a ManyToMany field is here:

customer_form.vehicle_id = current_vehicle.id

vehicle_id is defined as a ManyToMany field on your Customer model, therefore you can’t just assign a single id to it. You have to add an instance of VehicleSale model, eg:

customer_form.vehicle_id.add(current_vehicle)

See docs here:
https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

See also this answer for why you can’t save until you populate the vehicle_id relation:
https://stackoverflow.com/a/2529875/202168

Leave a comment