如何在 Django admin 中处理一种超类型的多个子类型

发布于 2024-08-25 02:56:23 字数 1137 浏览 8 评论 0原文

添加/编辑多个子类型的最佳解决方案是什么?

例如,超类型类Contact 具有子类型类Client 和子类型类Supplier。这里显示的方式是有效的,但是当您编辑联系人时,您会同时获得内联,即子类型客户和子类型供应商。

因此,即使您只想添加客户,您也会获得供应商的字段,反之亦然。如果添加第三个子类型,您将获得三个子类型字段组,而实际上您只需要一个子类型组,在上述示例中:Client。

例如:

class Contact(models.Model):
    contact_name = models.CharField(max_length=128)

class Client(models.Model):
    contact = models.OneToOneField(Contact, primary_key=True)
    user_name = models.CharField(max_length=128)

class Supplier(models.Model):
    contact.OneToOneField(Contact, primary_key=True)
    company_name = models.CharField(max_length=128)

在 admin.py 中

class ClientInline(admin.StackedInline):
    model = Client

class SupplierInline(admin.StackedInline):
    model = Supplier

class ContactAdmin(admin.ModelAdmin):
    inlines = (ClientInline, SupplierInline,)

class ClientAdmin(admin.ModelAdmin):
    ...

class SupplierAdmin(admin.ModelAdmin):
    ...

现在,当我想添加一个客户(即仅一个客户)时,我编辑联系人,并获得客户和供应商的内联。当然,供应商也是如此。

有办法避免这种情况吗?当我想添加/编辑客户时,我只能看到内联的客户,当我想添加/编辑供应商时,我只能看到内联的供应商,在添加/编辑联系人时?

或者也许有不同的方法。任何帮助或建议将不胜感激。

What would be the best solution for adding/editing multiple sub-types.

E.g a super-type class Contact with sub-type class Client and sub-type class Supplier. The way shown here works, but when you edit a Contact you get both inlines i.e. sub-type Client AND sub-type Supplier.

So even if you only want to add a Client you also get the fields for Supplier of vice versa. If you add a third sub-type , you get three sub-type field groups, while you actually only want one sub-type group, in the mentioned example: Client.

E.g.:

class Contact(models.Model):
    contact_name = models.CharField(max_length=128)

class Client(models.Model):
    contact = models.OneToOneField(Contact, primary_key=True)
    user_name = models.CharField(max_length=128)

class Supplier(models.Model):
    contact.OneToOneField(Contact, primary_key=True)
    company_name = models.CharField(max_length=128)

and in admin.py

class ClientInline(admin.StackedInline):
    model = Client

class SupplierInline(admin.StackedInline):
    model = Supplier

class ContactAdmin(admin.ModelAdmin):
    inlines = (ClientInline, SupplierInline,)

class ClientAdmin(admin.ModelAdmin):
    ...

class SupplierAdmin(admin.ModelAdmin):
    ...

Now when I want to add a Client, i.e. only a Client I edit Contact and I get the inlines for both Client and Supplier. And of course the same for Supplier.

Is there a way to avoid this? When I want to add/edit a Client that I only see the Inline for Client and when I want to add/edit a Supplier that I only see the Inline for Supplier, when adding/editing a Contact?

Or perhaps there is a different approach. Any help or suggestion will be greatly appreciated.

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

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

发布评论

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

评论(1

时光礼记 2024-09-01 02:56:23

如果不使用一对一的外键来联系您而是从它继承呢?

class Contact(models.Model):
    contact_name = models.CharField(max_length=128)

    class Meta:
        abstract=True # Don't use this line if you want Contact to have its own table

class Client(Contact):
    user_name = models.CharField(max_length=128)

class Supplier(Contact):
    company_name = models.CharField(max_length=128)

然后您可以注册客户和供应商,他们将共享联系人中的字段,但仍然彼此分开。

What if instead of using a one to one foreign key to contact you inherited from it instead?

class Contact(models.Model):
    contact_name = models.CharField(max_length=128)

    class Meta:
        abstract=True # Don't use this line if you want Contact to have its own table

class Client(Contact):
    user_name = models.CharField(max_length=128)

class Supplier(Contact):
    company_name = models.CharField(max_length=128)

Then you could register Client and Supplier, and they would share the fields from Contact but would still be separate from each other.

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