django - 如何在 Django ORM 中创建 2 个数据库之间的关系(一个非托管,一个托管)
问题描述
我有两个模型:
ciad_db
- MySQL,非托管
class NormalizedData(models.Model):
ticket_number = models.CharField(max_length=100, blank=True, primary_key=True, default='noticketid')
status = models.CharField(max_length=45, blank=True, null=True)
ticket_source = models.CharField(max_length=45, blank=True, null=True)
summary = models.TextField(blank=True, null=True)
conclusion = models.ForeignKey(DsrConclusions, on_delete=models.CASCADE, default=None)
class Meta:
managed = False
db_table = 'normalized_data'
indexes = [
models.Index(
fields=[
'status',
'ticket_source',
'summary',
]
)
]
default
- sqlite3,托管
class DsrConclusions(models.Model):
id = models.IntegerField(primary_key=True)
ticket_number = models.CharField(max_length=100, blank=False, null=False, unique=True)
explanation = models.TextField(blank=False, null=False)
final_breach_conclusion = models.BooleanField(blank=False,null=False)
last_owner = models.CharField(blank=False,null=False, max_length=50)
class Meta:
managed = True
indexes = [
models.Index(
fields=[
'ticket_number',
'explanation',
'final_breach_conclusion',
'last_owner',
]
)
]
in models.py
,DsrConclusions
在之前进来NormalizedData
由于有 2 个数据库,我使用数据库路由器,如下所示:
class CiadRouter(object):
"""
A router to control all database operations on models in
the ciadseeweb application
"""
def db_for_read(self, model, **hints):
"""
Point all operations on ciadseeweb models to 'ciad_db'
"""
if model._meta.app_label == 'ciadseeweb':
return 'ciad_db'
return None
def db_for_write(self, model, **hints):
"""
Point all operations on myapp models to 'other'
"""
if model._meta.app_label == 'ciadseeweb':
return 'default'
return None
def allow_syncdb(self, db, model):
"""
Make sure the 'ciadseeweb' app only appears on the 'other' db
"""
if db == 'ciad_db':
return model._meta.app_label == 'ciadseeweb'
elif model._meta.app_label == 'ciadseeweb':
return False
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Relations between objects are allowed if both objects are
in the primary/replica pool.
"""
db_list = ('default', 'ciad_db')
if obj1._state.db in db_list and obj2._state.db in db_list:
return True
return None
由于我在迁移完成后添加了该列(并且一切正常),一旦我添加了该列并运行makemigrations
,我得到No changes detected
,这是因为它NormalizedData
是非托管的。然后我将其更改为托管,makemigrations
再次运行并让他们检测更改。一旦我运行migrate
,它们就适用了。
当我运行服务器(启动时没有错误)并刷新我将NormalizedData
数据呈现到django-tables2
带有django_tables2_column_shifter
表的页面时。
为了从模型中获取数据,从DsrConclusions
模型中tables.py
渲染NormalizedData
。我正在使用dsr_review = tables.Column(verbose_name='DSR SLA', accessor='dsrconclusions.final_breach_conclusion')
,如django-tables2
文档中所述。
我最终得到:
django.db.utils.OperationalError: (1054, "Unknown column 'normalized_data.conclusion_id' in 'field list'")
我被困住了。我究竟做错了什么?!这甚至可能吗?
环境
Package Name Version
Django 2.2
Python 3.7.2
Crispy_Forms 1.7.2
Debug Toolbar 1.11
Django_Tables2 2.0.6
Django_Tables2_Column_Shifter 0.5.2
解决方案
推荐阅读
- dialogflow-es - Dialogflow“重复”所有意图的后续行动
- django - 您好,我需要帮助,我想在父母类别中显示帖子总数,我该怎么做?
- postgresql - 如何从外部网络查看 postgresql 数据库中的所有表?
- jquery - 选择一个 div,如果您单击该 div,或者一个子元素
- python - 将迭代器中的字符串转换为 Python 列表
- android - React 本机 android APK 发布构建失败
- ios - 检查字体只是粗体而不是额外粗体、半粗体或任何其他粗体
- ruby-on-rails - 如何克隆哈希数组并使用每个循环添加键值
- python - 从 Panda Dataframe 中提取内容
- c# - 在 .NET Framework 项目的 Azure 管道中生成的 NuGet 依赖项版本不正确