Jamgreen
Jamgreen

Reputation: 11059

Use foreign keys in unmanaged model in Django

I have some unmanaged models to get data from an Oracle database, but I cannot read anything in the documentation about joining tables.

I have these models

class Model1(models.Model):
    id = models.CharField(max_length=200, primary_key=True)
    name = models.CharField(max_length=200, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'table_1_name'

class Model2(models.Model):
    model1_id = models.CharField(max_length=200, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'table_2_name'

and I'm trying to join the tables in model 1 and model 2.

I thought I could use model1 = models.ForeignKey(Model1) in Model2 to get data with obj.model1__name, but I cannot make it work.

Upvotes: 2

Views: 1338

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599816

The problem isn't anything to do with unmanaged models - it's just a question of syntax.

The ForeignKey declaration is correct, but accessing the related information is done via the dot syntax: obj.model1.name.

Upvotes: 3

Related Questions