[Answer]-Update ManyToManyField in Django from form data

1👍

With reference to modelForm documentation, when you are using modelForms you don’t need so much of code, your code would reduce to:

guest = get_object_or_404(Guest, id=guest_id)
form = GuestForm(instance=guest)

Again for post,

#get the guest instance, if updating existing record
form = GuestForm(request.POST, instance=guest)  
if form.is_valid():
    new_guest = form.save(commit=False) #you want to set excluded fields
    new_guest.user = request.user
    #new_guest.invitation_date = somedate
    new_guest.save()
    form.save_m2m() #should do this if save(commit=False) used
👤Rohan

Leave a comment