【问题标题】:Simple Django form / model save question简单的 Django 表单/模型保存问题
【发布时间】:2010-09-06 15:24:52
【问题描述】:

我想在保存 ModelForm 时将 BooleanField 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's answer)。它仍然没有在提交/保存时将 inuse 更新为 True。

【问题讨论】:

  • 还有其他型号吗? Booking 表单的底层模型是什么? Location和其他型号有什么关系?
  • 除了你看到的减去一些元等之外没有任何关系
  • models.BooleanField(default=True) 有什么问题?
  • 这些位置是由管理员使用 False 添加的,我只希望当用户在预订表单上选择它们时将它们更新为 True。

标签: python django django-models django-forms


【解决方案1】:
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()

【讨论】:

  • @Daniel:不应该是Location.inuse吗?根据 OP Booking 是一个 form.
  • 看起来很有希望,但给了我...TypeError at / super(type, obj): obj must be an instance or subtype of type
  • @Rob B:尝试用booking = super(BookingForm, self).save(commit=False)替换booking = super(Booking, self).save(commit=False)
  • @Manoj Govindan 解决了我的错误消息,但是在我提交表单时,在管理员中查看 Location 值尚未更新为 true。
  • @Rob B:底层模型是Booking还是Location?丹尼尔的回答假定是Booking;但我在您的问题中没有看到这样的模型。
【解决方案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

【讨论】:

  • 不会出现任何错误,只是不会在保存时将位置值更新为 True
  • 不,仍然没有运气。没有错误,但似乎也没有做任何事情
  • 搞定了。往上看。诀窍是显式调用self.save_m2m() 来保存m2m 对象。
  • 你能粘贴整个模型以便我比较吗?我已经检查了三倍,但它不起作用
  • @Manoj Govindan 谢谢。仍然没有运气。我的观点会影响什么吗?
猜你喜欢
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 2011-01-08
  • 2011-11-07
相关资源
最近更新 更多