Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 简单Django表单/模型保存问题_Python_Django_Django Models_Django Forms - Fatal编程技术网

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

Python 简单Django表单/模型保存问题,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我想在保存ModelForm时将BooleanFieldinuse设置为True(我使用的是管理区域之外的表单),但我不确定如何操作 型号: class Location(models.Model): place = models.CharField(max_length=100) inuse = models.BooleanField() class Booking(models.Model): name = models.CharField(max_length=1

我想在保存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()
视图:

更新至最新版本(请参阅)。在提交/保存时,它仍然没有将inuse更新为True。

以下是我的建议:

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

还有其他的型号吗?
预订
表单的基本模型是什么?
Location
与其他模型(如果有的话)之间的关系是什么?除了您看到的减去一些元etc之外,没有其他关系
models有问题。BooleanField(默认值=True)
?管理员用False添加位置,我只希望在用户在预订表单上选择位置时将其更新为True。@丹尼尔:不应该是
位置。使用
?根据OP
Booking
是一个表单。看起来很有希望,但给了我…TypeError at/super(type,obj):obj必须是type@RobB:尝试将
booking=super(booking,self).保存(commit=False)
替换为
booking=super(BookingForm,self).保存(commit=False)
@Manoj Govindan解决了我的错误消息,但在我提交表单时,在管理中查找位置值尚未更新为true。@Rob B:表单的基础模型是
预订
还是
位置
?Daniel的回答假定是预订
;但我在你的问题中没有看到这样的模型。不会出现任何错误,只是在savenope上没有将位置值更新为True仍然没有运气。不会出错,但似乎什么都不做,只要能正常工作。见上文。诀窍是显式调用self.save_m2m()
来保存m2m对象。您能粘贴整个模型以便我进行比较吗?我已经检查了三倍了,不是吗working@Manoj戈文丹,谢谢。还是不走运。我的观点会影响什么吗?
# 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