0

I can't quite figure out how to work ou ManyToMany relationships in a legacy DB.

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'

The error is "Torre object has no attribute trecho__set", when I try to run the following query :

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

If I'm not mistaken, this would work if all these tables were managed by Django, but since they are legacy, how can I make this work?

Mojimi
  • 2,561
  • 9
  • 52
  • 116

0 Answers0