开发者

Simple Django form / model save question

开发者 https://www.devze.com 2023-01-14 20:21 出处:网络
I want to set the BooleanField inuse to True when I save the ModelForm (I\'m using a form outside of the admin area) and I\'m unsure how to do it.

I want to set the BooleanField inuse to True when I save the ModelForm (I'm using a form outside of the admin area) and I'm unsure how to do it.

Models:

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

class Booking(models.Model):
    name = models.CharFiel开发者_如何学Cd(max_length=100, verbose_name="Your name*:")
    place = models.ManyToManyField(Location, blank=True, null=True)

Forms:

class BookingForm(ModelForm):

    class Meta:
        model = Booking

        def save(self, commit=True):
            booking = super(BookingForm, self).save(commit=False)
            if commit:
                booking.save()
                self.save_m2m()
                for location in booking.place.all():
                    location.inuse = True
                    print location #nothing prints
                    location.save()

View:

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

        return render_to_response('bookingform.html', {
                'form': form,
            })

Updated to latest (see Manoj Govindan's answer). It is still not updating inuse to True on submit / save.


class BookingForm(ModelForm):

    class Meta:
        model = Booking

    def save(self, commit=True):
        booking = super(BookingForm, self).save(commit=False)
        booking.inuse = True
        if commit:
            booking.save()


Here is my stab at it:

class BookingForm(ModelForm):
    class Meta:
        model = Booking

    def save(self, commit=True):
        booking = super(BookingForm, self).save(commit=False)
        if commit:
            booking.save()  
            self.save_m2m()
            for location in booking.place.all():
                location.inuse = True
                location.save()

Update

Entire code I've used:

# 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)

# forms.py
class BookingForm(ModelForm):
    class Meta:
        model = Booking

    def save(self, commit=True):
        booking = super(BookingForm, self).save(commit=False)
        if commit:
            booking.save()
            self.save_m2m()
            for location in booking.place.all():
                location.inuse = True
                location.save()

In [1]: from test_app.forms import BookingForm
In [2]: from test_app.models import Location

# I had already saved some `Location` instances.

In [3]: data = dict(name = 'MyCity', place = [p.id for p in Location.objects.all()])
In [4]: f = BookingForm(data)
In [5]: f.save()
In [6]: for each in Location.objects.all():
   ...:     print each.place, each.inuse
   ...:      
PlaceA True 
PlaceB True 
PlaceC True
0

精彩评论

暂无评论...
验证码 换一张
取 消