【问题标题】:Django model class inheritance with non managed classesDjango 模型类继承与非托管类
【发布时间】:2013-09-10 22:10:48
【问题描述】:

我有一个简单的 REST API,我想使用 Django 和 DJANGO REST 框架。首先,我需要连接到 2 个不同的远程数据库,并从每个数据库中获取唯一国家/地区的记录集。然后我想将这些记录集组合成一个模型类。我想将每个记录集保存在单独的类中,以便在 API 的其他区域中使用。

我尝试过使用模型类继承,但我无法让它与非托管表一起使用。这是模型的最新版本。

class CountryA(models.Model):
    Country = models.CharField(db_column='field_country_country_value',primary_key = True, max_length=255)
    class Meta:
        abstract = True
        managed = False

class CountryB(models.Model):
    Country = models.CharField(primary_key = True, max_length=255)
    class Meta:
        abstract = True
        managed = False

class CombinedCountries(ACountry,BCountry): 
    class Meta:
        managed = False

Django 似乎仍在为 CombinedCountires 寻找本地表。我也尝试过作为没有抽象的代理表,但它会从 CountryA 中查找 mcapi.content_field_country_country 作为表名的字段。我确信必须有一种方法可以在带有远程表的模型中执行此操作,但这显然不是一个常见的用例。

【问题讨论】:

    标签: python django model multiple-inheritance django-rest-framework


    【解决方案1】:

    我认为更好的解决方案是链接两个模型的结果并将最终结果用作 API 的输入列表。

    How to combine 2 or more querysets in a Django view?

    【讨论】:

    • 是的,我们在视图中做了类似的事情,问题是我们使用的是 REST 框架中的 ViewSet,它需要将 QuerySet 传递给序列化函数。
    • 基本上我们所做的是将链结果导入到数据模型中,因为它是自己的托管表。不完全是我们想要的,因为它创建了一个重复的数据存储,但足够接近。
    猜你喜欢
    • 1970-01-01
    • 2019-07-14
    • 2022-06-24
    • 2015-03-05
    • 2013-04-20
    • 1970-01-01
    • 2017-11-07
    • 2014-07-02
    • 1970-01-01
    相关资源
    最近更新 更多