Django对象在旧数据库中没有属性“__set”

时间:2017-02-15 19:35:32

标签: python django django-models django-1.10

我无法弄清楚如何在遗留数据库中处理许多关系。

models.py:

class Trecho(models.Model):
    cod = models.BigIntegerField(primary_key=True)
    torres = models.ManyToManyField('Torre', through="Trecho_Com_Torre")

    class Meta:
        managed = False
        db_table = 'OPE_TRECHO'

class Torre(models.Model):
    cod = models.BigIntegerField(primary_key=True)

    class Meta:
        managed = False
        db_table = 'OPE_TO'

class Trecho_Com_Torre(models.Model):
    cod = models.BigIntegerField(primary_key=True)
    cod_trecho = models.ForeignKey('Trecho', on_delete=models.CASCADE, db_index=False, db_column='COD_TRECHO')
    cod_to = models.ForeignKey('Torre', on_delete=models.CASCADE, db_index=False, db_column='COD_TO')

    class Meta:
        managed = False
        db_table = 'OPE_TRECHO_COM_TO'

当我尝试运行以下查询时,错误是“Torre对象没有属性trecho__set”:

t1 = Torre.objects.get(cod=1)
trechos = t1.trecho__set.all()

如果我没有弄错的话,如果所有这些表都是由Django管理的话,这会有效,但由于它们是遗留的,我怎样才能使这个工作?

0 个答案:

没有答案