詹戈。有限域集的代理模型

2024-10-03 23:29:45 发布

您现在位置:Python中文网/ 问答频道 /正文

主矩-数据库方案不是从头开始设计的。它是从遗留系统继承而来的,必须保持当前的状态,因为它还与一些外部系统共享。所以我们有:

class A(models.Model): 
    """
    List of 110 fields
    """
    field_1 = models.CharField()
    field_... = models.CharField()
    field_n = models.CharField()

有没有办法让模型B成为代理模型,并且有有限的字段集?在

比如:

^{pr2}$

我目前想到的唯一解决方案是创建一些非托管模型。但是代理模型看起来更适合这个目标。。还是没有?在


Tags: of模型数据库field代理fieldsmodelmodels
1条回答
网友
1楼 · 发布于 2024-10-03 23:29:45

根据docs的说法,代理模型并不是您想要的

So, the general rules are:

If you are mirroring an existing model or database table and don’t want all the original database table columns, use Meta.managed=False. That option is normally useful for modeling database views and tables not under the control of Django.

If you are wanting to change the Python-only behavior of a model, but keep all the same fields as in the original, use Meta.proxy=True. This sets things up so that the proxy model is an exact copy of t he storage structure of the original model when data is saved.

最好的办法是在SQL中创建一个只列出所需字段的视图,然后使用非托管表连接到该视图。在

相关问题 更多 >