简单的Django表单/模型保存问题

7 投票
2 回答
5361 浏览
提问于 2025-04-16 03:46

我想在保存这个ModelForm的时候,把布尔字段inuse设置为True(我是在管理区域外使用这个表单),但我不知道该怎么做。

模型:

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)

表单:

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

视图:

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,
            })

已经更新到最新版本(见Manoj Govindan的回答)。但在提交/保存时,inuse仍然没有更新为True。

2 个回答

2

这是我对这个问题的尝试:

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

更新

我用的完整代码如下:

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

在编程中,有时候我们需要让程序做一些重复的事情,比如计算、处理数据等等。为了让程序更高效,我们可以使用循环。循环就像是一个指令,告诉程序“请重复做这件事,直到满足某个条件”。

比如说,如果你想让程序从1数到10,你可以用循环来实现,而不是手动写出每一个数字。这样不仅省事,还能减少出错的机会。

循环有几种不同的类型,最常见的有“for循环”和“while循环”。“for循环”通常用来处理已知次数的重复,而“while循环”则是在不知道具体次数的情况下使用,直到某个条件不再满足为止。

总之,循环是编程中一个非常重要的工具,它能帮助我们更高效地完成任务。

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

撰写回答