我有一个 Django 应用程序,它必须使用另一个来源提供的数据库。 我无法更改架构。以下所有内容均由 inspectdb 生成:
class Family(models.Model):
persistanceid = models.BigIntegerField(primary_key=True)
class Spouseref(models.Model):
persistanceid = models.BigIntegerField(primary_key=True)
class Family_spouseref(models.Model):
family_persistanceid = models.ForeignKey(Family, db_column='family_persistanceid')
wiferefs_persistanceid = models.ForeignKey(Spouseref, unique=True, null=True, db_column='wiferefs_persistanceid', blank=True, related_name='wiferefs')
husbandrefs_persistanceid = models.ForeignKey(Spouseref, unique=True, null=True, db_column='husbandrefs_persistanceid', blank=True, related_name='husbandrefs')
childrefs_persistanceid = models.ForeignKey(Spouseref, unique=True, null=True, db_column='childrefs_persistanceid', blank=True, related_name='childrefs')
ManyToManyField 在 Family 下不起作用,因为映射表 Family_spouseref 有多个外键到 Spouseref。给定这个模式,完成这样的事情的最有效方法是什么:
fam = Family.objects.get(persistanceid=id)
husbands = fam.husbands.all()
wives = fam.wives.all()
children = fam.children.all()
更新:
根据要求...以下功能是我尝试在上面的家庭模型中添加的。
我尝试了这样的原始 SQL:
def husbands(self):
return Spouseref.objects.raw ('SELECT * FROM spouseref WHERE spouseref.persistanceid IN (SELECT husbandrefs_persistanceid FROM family_spouseref WHERE family_persistanceid=%s);', [self.persistanceid])
哪个有效,但原始 SQL 真的是最好的方法吗?看起来,我以这种方式失去了很多常用的对象方法。
当我尝试通过 ORM 执行此操作时,它一直想认为 Family_spouseref 表有一个列 id,而它显然没有。
Family_spouseref.objects.get(family_persistanceid=self.persistanceid)
出品:
[DEBUG] django.db.backends: (0.001) SELECT "family_spouseref"."id", "family_spouseref"."family_persistanceid", "family_spouseref"."wiferefs_persistanceid", "family_spouseref"."husbandrefs_persistanceid", "family_spouseref"."childrefs_persistanceid" FROM "family_spouseref" WHERE "family_spouseref"."family_persistanceid" = 45036 LIMIT 21; args=(45036,)
DatabaseError: column family_spouseref.id does not exist
LINE 1: SELECT "family_spouseref"."id", "family_spouseref"."family_p...
我也试过:
Family_spouseref.objects.get(Q(family_persistanceid=self.persistanceid) | Q(husbandrefs_persistanceid__isnull=False))
这产生了相同的 ID 错误,但确实尝试遵循 FK 关系:
[DEBUG] django.db.backends: (0.000) SELECT "family_spouseref"."id", "family_spouseref"."family_persistanceid", "family_spouseref"."wiferefs_persistanceid", "family_spouseref"."husbandrefs_persistanceid", "family_spouseref"."childrefs_persistanceid" FROM "family_spouseref" LEFT OUTER JOIN "spouseref" ON ("family_spouseref"."husbandrefs_persistanceid" = "spouseref"."persistanceid") WHERE ("family_spouseref"."family_persistanceid" = 45036 OR "spouseref"."persistanceid" IS NOT NULL) LIMIT 21; args=(45036,)
DatabaseError: column family_spouseref.id does not exist
LINE 1: SELECT "family_spouseref"."id", "family_spouseref"."family_p...
通过调试看到它调用相同(错误)语句(8 次)的频率与它调用两次的原始 SQL 相比,我也感到震惊。(仍然,两次!)
我真的很想知道遍历这些表的官方 Django 方式,而不是完全忽略 ORM 并执行原始 SQL。谢谢。