访问save_formset和save_model的父对象字段

时间:2012-06-08 09:21:08

标签: django forms django-admin django-forms inline-formset

我有一个ActionAdmin模型,在InvoiceAdmin模型中用作ActionAdminInline。

在InvoiceModel中,我覆盖save_formset,自动设置一些值。设置每个Action实例的值没有问题,但我无法获取父Invoice的字段。

我认为必须自我 self.project.pk 我也尝试通过其他一些帖子中的 form.project.pk 来获取它。

我得到的错误是InvoiceAdmin中没有这样的字段。这对我来说很有意义,self是InvoiceAdmin对象而不是Invoice对象。我想我必须得到Invoice对象字段来设置Action instance.invoice和instance.project。

有人知道,如何获得这个父对象值???

看起来像这样:

class ActionAdmin(admin.ModelAdmin):
...


class ActionInlineForm(ModelForm):
    class Meta:
        model = Action
        fields = ['name','tax', 'price','duration_extern',]


class ActionInline(admin.TabularInline):
    model = Action
    form = ActionInlineForm
    extra = 0
    ordering = ('date_finished',)
    can_delete=False
    readonly_fields = ['non_editable_date_finished','non_editable_duration','get_remove_invoice_pos_link']




class InvoiceAdmin(admin.ModelAdmin):
...
...
...
  inlines = [ActionInline,]
  ...
  ...
  ...
  def save_formset(self, request, form, formset, change):
      instances = formset.save(commit=False)
      for instance in instances:
          usr = User.objects.get(id=7)
          try:
            instance.created_by = instance.created_by
          except:
            instance.created_by = usr

          try:
            instance.owner = instance.owner
          except:
            instance.owner = usr

          instance.modified_by = usr

          try:
            instance.date_created = instance.date_created
          except:
            instance.date_created = date.today

          instance.date_modified = date.today:

          ### MAN, This WORKS NOW ###
          try:
              instance.project = instance.project
          except:
            pr = Project.objects.get(id=instance.invoice.project.id)
            instance.project = pr


          actstat = ActionStatus.objects.get(id=2)
          instance.actionstatus = actstat

          try:
            instance.actioncategory = instance.actioncategory
          except:
            cat = ActionCategory.objects.get(id=9)
            instance.actioncategory = cat

          instance.done = True
          instance.billed = True    
          instance.save()
      formset.save_m2m()

这是错误消息:

AttributeError at /workflow/invoice/21/
    ...
    Exception Type:     AttributeError
    Exception Value:    'InvoiceAdmin' object has no attribute 'id'
    ...

Traceback
    form <django.forms.models.InvoiceForm object at 0x7f7f7421de10>
    instances [<Action: aaaaa>]
    self <workflow.admin.InvoiceAdmin object at 0x7f7f7421dad0>

问题是,自我对象是InvoiveAdmin而不是Invoice?我的意思是实例对象是Action而不是ActionAdmin?!

1 个答案:

答案 0 :(得分:0)

行。我找到了instance.invoice的解决方案。它是正确设置的,没有做任何例子.invoice。我从上面的代码中删除了“instance.invoice = self”行。

通过父对象获取项目的解决方案,即发票由“instance.invoice.project.id”完成

所以我遍历表格动作发票到投影而不是它的ID。上面也改变了这一点。

使用try检查实例字段是必要的,除了。首先我尝试了如果不是instance.project
如果不是hasatrr(实例,'项目')

if instance.project =“”
但无异常处理会引发 DoesNotExists错误。将其从 if / else 更改为上面的尝试/除


顺便说一句

真的很少见。我只是在ActionAdmin save_model 中更改了一些内容
当我尝试在那里推出实例字段时,与save / formset中的try / except相同,我不起作用。

在save_model中我必须这样做:

 if not instance.price:
        instance.price = instance.project.customer.price

这失败了

  try:
      instance.price = instance.price
  except:
      instance.price = instance.project.customer.price

在save_formset中它反之亦然? !

有人可以向我解释一下吗?