禁止使用save()以防止因未保存的相关对象

时间:2015-11-21 00:51:11

标签: python django

我需要将新创建的ModelForm中的主键传递到同一视图中的另一个表单域,但是我收到错误。有什么建议使这项工作? 看起来像过去,这就是答案:

def contact_create(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse(contact_details, args=(form.pk,)))
    else:
        form = ContactForm()

从文档中,这是较新的Django版本> 1.8.3

中发生的情况
  

p3 =地点(姓名='恶魔狗',地址=' 944 W. Fullerton')   Restaurant.objects.create(place = p3,serve_hot_dogs = True,serve_pizza = False)
  追溯(最近的呼叫最后):
  ...
  ValueError:禁止使用save()来防止由于未保存的相关对象而造成的数据丢失'地点'

这就是我从视图中获取pk的方式:

my_id = ""
if form.is_valid():
    # deal with form first to get id
    model_instance = form.save(commit=False)
    model_instance.pub_date= timezone.now()
    model_instance.user= current_user.id
    model_instance.save()
    my_id = model_instance.pk

if hourformset.is_valid():
    hourformset.save(commit=False)
    for product in hourformset:
        if product.is_valid():
            product.save(commit=False)
            product.company =  my_id
            product.save()
else:
    print(" modelform not saved")
return HttpResponseRedirect('/bizprofile/success')

3 个答案:

答案 0 :(得分:15)

这是在Django 1.8中引入的。以前,您可以将未保存的实例分配给一对一关系,如果失败,则会以静默方式跳过。从Django 1.8开始,在这种情况下您将收到错误消息。 检查Django 1.7的documentation - > 1.8升级。

它说:

  

将未保存的对象分配给ForeignKey,GenericForeignKey和   OneToOneField现在引发了一个ValueError。

如果您对更多详情感兴趣,可以在save中查看django.db.models.base方法:部分内容:

for field in self._meta.concrete_fields:
    if field.is_relation:
        # If the related field isn't cached, then an instance hasn't
        # been assigned and there's no need to worry about this check.
        try:
            getattr(self, field.get_cache_name())
        except AttributeError:
            continue
        obj = getattr(self, field.name, None)
        # A pk may have been assigned manually to a model instance not
        # saved to the database (or auto-generated in a case like
        # UUIDField), but we allow the save to proceed and rely on the
        # database to raise an IntegrityError if applicable. If
        # constraints aren't supported by the database, there's the
        # unavoidable risk of data corruption.
        if obj and obj.pk is None:
            raise ValueError(
                "save() prohibited to prevent data loss due to "
                "unsaved related object '%s'." % field.name
            )

最后5行是引发此错误的位置。基本上您未保存的相关obj将会obj.pk == NoneValueError将会被提升。

答案 1 :(得分:10)

很简单:

p3 = Place(name='Demon Dogs', address='944 W. Fullerton')   
p3.save() # <--- you need to save the instance first, and then assign
Restaurant.objects.create(
    place=p3, serves_hot_dogs=True, serves_pizza=False
) 

答案 2 :(得分:3)

已回答 - 问题产生于django没有保存空或未更改的表单。这导致那些未保存的表单上的空字段。通过在外键上允许空字段来解决问题 - 事实上 - 所有字段。这样,空或未更改的表单在保存时不会返回任何错误。

仅供参考:请参阅@wolendranh answer。