python - Django 。字段集有限的代理模型

标签 python mysql django django-models

主要时刻 - 数据库方案不是从头开始设计的。它是从遗留系统继承而来的,目前必须保持原样,因为它也与一些外部系统共享。所以我们有:

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

有什么方法可以拥有模型 B,它将是代理模型并且字段集有限?

喜欢:

class B(A):
    class Meta: 
        proxy = True
    field_3 = models.CharField()
    field_5 = models.CharField()
    field_10 = models.CharField()

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

最佳答案

根据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 中创建一个 View ,仅列出您想要的字段,然后使用非托管表连接到它。

关于python - Django 。字段集有限的代理模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38414153/

相关文章:

python - SWIG Python undefined symbol 错误

python - 慢慢改变 BigQuery 的查找缓存 - Dataflow Python Streaming SDK

mysql - 有什么办法可以加快 MySQL 查询速度吗?

python - Django:用于开发的 sqlite,用于生产的 mysql?

Python:内联 if 语句 else 什么都不做

python - 创建django权限,报错: ContentType matching query does not exist

python - 未知命令 : 'clearsessions'

python - 如何解释链式比较操作的抽象语法树?

python - 在 HoverTool 中格式化

mysql - 我该如何修复它 : Mysql remote access not working