如何在 Django admin 中处理一种超类型的多个子类型
添加/编辑多个子类型的最佳解决方案是什么?
例如,超类型类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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不使用一对一的外键来联系您而是从它继承呢?
然后您可以注册客户和供应商,他们将共享联系人中的字段,但仍然彼此分开。
What if instead of using a one to one foreign key to contact you inherited from it instead?
Then you could register Client and Supplier, and they would share the fields from Contact but would still be separate from each other.