Django (admin.e104) 必须继承自“InlineModelAdmin”

Naa*_*Iss 8 django django-admin

尝试将一些模型作为内联模型放入其他两个模型会产生一些奇怪的错误(在标题中)。

我有两种订单模型 - 远程和本地。并且在每一个中,我都需要插入无限数量的产品。另一个目的是在不同列表中显示本地和远程订购的产品的完整列表。

第二个目标的解决方案:使用从第一个继承的第二个产品模型及其自己的经理(对象)。

目标一的解决方案:在订单的管理类中使用内联来按顺序附加正确的产品(本地产品到本地订单,远程产品到远程订单)。

将本地产品的内联添加到本地购买的管理员效果很好。但是将远程产品的内联添加到远程订单管理文件的内联字段会为远程订单的管理类返回错误:

must inherit from 'InlineModelAdmin'.
Run Code Online (Sandbox Code Playgroud)

即使从本地订单的管理文件中删除内联,它也会返回错误。

在models.py中有一些像这样的代码:

must inherit from 'InlineModelAdmin'.
Run Code Online (Sandbox Code Playgroud)

在 admin.py

class RemotePurchaseModel(models.Model):
    title = models.CharField
    number = models.IntegerField
    ...

class LocalPurchaseModel(models.Model):
    title = models.CharField
    number = models.IntegerField
    ...

class LocalProductModel(models.Model):
    remote = models.BooleanField(default=False)
    actual_order = models.ForeignKey(to=LocalPurchaseModel, blank=True, null=True)
    remote_purchase = models.ForeignKey(to=RemotePurchaseModel, blank=True, null=True)
    ...

class RemoteProductManager(djando.db.models.Manager):
    def get_queryset(self):
        return super(PurchasedProductManager, self).get_queryset().filter(remote=True)

class RemoteProductModel(LocalProduct):
    proxy = True
    objects = RemoteProductManager
    ...

...
Run Code Online (Sandbox Code Playgroud)

而且这种变化的原因是两个在管理端显示本地和远程产品的不同字段集。当然,LocalProductModel 中存在所有字段。有什么线索吗?谢谢。

rar*_*iru 24

My stupid mistake was that I had the inline class name inside quotes.

instead of:

class MyAdmin(admin.ModelAdmin):
    inlines = [MyInlineAdmin]
Run Code Online (Sandbox Code Playgroud)

it was:

class MyAdmin(admin.ModelAdmin):
    inlines = ['MyInlineAdmin']
Run Code Online (Sandbox Code Playgroud)

Total Cost:

  • 2 Hours
  • Some scratches on the laptop screen
  • Less swimming today in the sea

  • 我做了同样的事情。感谢您的牺牲! (2认同)

Naa*_*Iss 1

找到原因了。在实际代码中(最初不是我的),模型的名称与它们所在的文件的名称相同。儿子在检查 Django(或 python)的某个步骤中检测到从 LocalProductModel 继承 RemoteProductModel 为错误 - 就像从文件继承一样,不是类模型。之后,它无法使用其字段,当然,如果远程产品,RemotePurchaseAdmin 无法像模型一样进行内联导入。它不会警告继承错误,但会对尝试导入不正确的模型或内联模型的 admin.ModelAdmin 发出 admin.E104 警告。

感谢大家。希望这能警告其他开发人员不要犯愚蠢的错误。