Django模型继承

发布于 2024-12-18 14:06:05 字数 1882 浏览 3 评论 0原文

我遇到了一个 django 错误,它把我的头发扯掉了。背景:我有一组相互继承的模型,并且我正在尝试构建一组具有并行结构的表单。

这是对象创建表单的基本类型:

class CreateSharedObjectForm(ModelForm): 
  def save(self, status, object_type, commit=True, *args, **kwargs):
    print "*********Got here!!!**************"
    shared_object = super(ModelForm,self).save( commit=False, *args, **kwargs)
    shared_object.status = status
    shared_object.object_type = object_type

    if commit:
      shared_object.save()
    return shared_object

这是继承的表单类型:

class NewBatchForm(CreateSharedObjectForm):
  def save(self, status, object_type, batch_options, commit=True, *args, **kwargs):
    print "Checkpoint A"
    batch = super(CreateSharedObjectForm,self).save( status, object_type, commit=False, *args, **kwargs )
    print "Checkpoint B"

    if commit:
      batch.save(*args, **kwargs)
    return analysis

  class Meta:
    model = batch

我从视图脚本调用继承的类型:

 form = NewAnalysisForm(request.POST, request.FILES)
  new_analysis = form.save(
    status = 'X',
    object_type  = 'Batch',
    batch_type = 'temp',
  )

它会抛出此错误:

save() takes at most 2 non-keyword arguments (4 given)

如果我将“super”行更改为:

batch = super(CreateSharedObjectForm,self).save( status, object_type, commit=False, *args, **kwargs )

我收到此错误:

Exception Type:     IntegrityError
Exception Value:    null value in column "parent_project_id" violates not-null constraint

Even wierder ,django 的跟踪输出给了我这个:

Checkpoint A
Checkpoint B

在返回 HTTP 500 错误之前。

据我所知,NewBatchForm 中 save 方法中的超级行永远不会调用 CreateSharedObjectForm。我知道 super 方法可能很棘手,但这只是单一继承,我可以不明白为什么超类的方法永远不会被调用。

这是怎么回事?我该如何修复它?

I've run into a django error that's got tearing out my hair. Background: I have a set of models inheriting from each other, and I'm trying to build a set of forms with a parallel structure.

Here's the base type for an object creation form:

class CreateSharedObjectForm(ModelForm): 
  def save(self, status, object_type, commit=True, *args, **kwargs):
    print "*********Got here!!!**************"
    shared_object = super(ModelForm,self).save( commit=False, *args, **kwargs)
    shared_object.status = status
    shared_object.object_type = object_type

    if commit:
      shared_object.save()
    return shared_object

Here's an inherited form type:

class NewBatchForm(CreateSharedObjectForm):
  def save(self, status, object_type, batch_options, commit=True, *args, **kwargs):
    print "Checkpoint A"
    batch = super(CreateSharedObjectForm,self).save( status, object_type, commit=False, *args, **kwargs )
    print "Checkpoint B"

    if commit:
      batch.save(*args, **kwargs)
    return analysis

  class Meta:
    model = batch

I call the inherited type from a view script:

 form = NewAnalysisForm(request.POST, request.FILES)
  new_analysis = form.save(
    status = 'X',
    object_type  = 'Batch',
    batch_type = 'temp',
  )

And it throws this error:

save() takes at most 2 non-keyword arguments (4 given)

If I change the "super" line to this:

batch = super(CreateSharedObjectForm,self).save( status, object_type, commit=False, *args, **kwargs )

I get this error:

Exception Type:     IntegrityError
Exception Value:    null value in column "parent_project_id" violates not-null constraint

Even wierder, django's trace output gives me this:

Checkpoint A
Checkpoint B

Before returning a HTTP 500 error.

As far as I can tell, the super line in the save method in NewBatchForm is never calling CreateSharedObjectForm. I'm aware that the super method can be tricky, but this is just single inheritance, and I can't figure out why the method for the superclass never gets called.

What's going on here? How do I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2024-12-25 14:06:05

您确定不想将 super(NewBatchForm, self).save 放在 NewBatchForm 中吗?

(你有super(CreateSharedObjectForm, self)

are you sure you don't want super(NewBatchForm, self).save inside NewBatchForm?

(you have super(CreateSharedObjectForm, self)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文