Python 保存(commit=False)引发验证错误

Python 保存(commit=False)引发验证错误,python,django,django-crispy-forms,Python,Django,Django Crispy Forms,我正在尝试使用以下代码处理图像上载: location = get_object_or_404(Location, slug=slug) if 'submit_photo' in request.POST: photo = PhotoForm(request.POST, request.FILES) if photo.is_valid(): new_photo = photo.save(commit=False) new_photo

我正在尝试使用以下代码处理图像上载:

    location = get_object_or_404(Location, slug=slug)
    if 'submit_photo' in request.POST:
    photo = PhotoForm(request.POST, request.FILES)
    if photo.is_valid():
        new_photo = photo.save(commit=False)
        new_photo.location = location
        new_photo.user = request.user
        new_photo.save()
        photo.save_m2m() 
    else:
        print(photo.errors)
当我尝试上传时,我得到一个验证错误,即位置是必填字段(它是)。但我认为commit=False的意义在于,我可以在保存任何内容之前添加信息,例如必填字段

我肯定我遗漏了一些非常愚蠢的东西,因为我几乎完全是从其他工作表单提交中复制过来的。我还添加了一些可能有用的其他代码

模型如下:

class Photo(models.Model):
    user = models.ForeignKey(User)
    location = models.ForeignKey(Location)
    title = models.CharField(max_length=30, blank=True, null=True)
    desc = models.CharField(max_length=150, blank=True, null=True, verbose_name='Description')
    created_on = models.DateTimeField(auto_now_add=True)
    photo = models.ImageField(upload_to='photos/%Y/%m/%d')
    tags = TaggableManager(blank=True)


    def __unicode__(self):
        return unicode(self.photo)
以下是表单(使用脆表单):

如果我将视图改写为:

    if 'submit_photo' in request.POST:
    photo = PhotoForm(request.POST, request.FILES)
    new_photo = photo.save(commit=False)
    new_photo.campground = campground
    new_photo.user = request.user
    new_photo.save()
    photo.save_m2m()  # REQUIRED TO SAVE TAGS
我在尝试上载时得到以下回溯:

Traceback:
File "/home/bobby/.virtualenvs/campthat3/lib/python2.7/site-            packages/django/core/handlers/base.py" in get_response
115.                         response = callback(request, *callback_args,    **callback_kwargs)
File "/home/bobby/django/campthat3/cg_profiles/views.py" in cg_profile
17.         new_photo = photo.save(commit=False)
File "/home/bobby/.virtualenvs/campthat3/lib/python2.7/site-   packages/django/forms/models.py" in save
370.                              fail_message, commit, construct=False)
File "/home/bobby/.virtualenvs/campthat3/lib/python2.7/site-  packages/django/forms/models.py" in save_instance
75.                          " validate." % (opts.object_name, fail_message))

Exception Type: ValueError at /michigan/bay-view/
Exception Value: The Photo could not be created because the data didn't validate.
根据您的评论:


当视图中有“if,else”时,我不会抛出错误页面,因为它只是转到“else”并在控制台中打印“location required”错误,而不是整个回溯

表单无效,因此即使使用
commit=False
,也无法保存表单。 表单中的错误可能是由于从模型中排除了字段,在这种情况下,不应将这些字段放入表单中

您可以在元类中使用
排除
字段
属性:

class PhotoForm(ModelForm):
    class Meta:
        model = Photo
        exclude = ['location', 'user']

你能追踪堆栈吗?追踪是你的意思吗?我是新来的,所以这就是我刚刚发布的。是的,那是什么意思:如果我去掉了“if,else”语句:当我在视图中有“if,else”时,我不会抛出错误页面,因为它只会转到“else”并在控制台中打印“location required”错误,而不是整个回溯,我不敢相信……我有错误表格上的“字段”部分被注释掉了……我已经看了两个小时了……我要睡觉了……谢谢
class PhotoForm(ModelForm):
    class Meta:
        model = Photo
        exclude = ['location', 'user']