views:

79

answers:

0

Utterly stuck trying to update a ManyToManyField field on POST and save.

Models.py

class Location(models.Model):
    place = models.CharField(max_length=100)
    inuse = models.BooleanField()

class Booking(models.Model):
    name = models.CharField(max_length=100, verbose_name="Your name")
    place = models.ManyToManyField(Location, blank=True, null=True, limit_choices_to = {'inuse':False})

Forms.py

class BookingForm(ModelForm):

    class Meta:
        model = Booking

    def save(self, commit=True):
        y = "def save is being called" #I don't get any print dialoge :(
        print y
        booking = super(BookingForm, self).save(commit=False)
        if commit:
            print booking
            booking.save()
            self.save_m2m()
            for location in booking.place.all():
                location.inuse = True
                print location
                location.save()

Views.py

def booking(request):
    form = BookingForm()
    if request.method == 'POST':
        form = BookingForm(request.POST)
        if form.is_valid():
            form.save()
  ...

The form should update Location's inuse to True on save but doesn't. I don't get any errors. What am I doing wrong?!

p.s. Massive thanks to @Manoj Govindan for helping me get this far.