Q:

django join with two foregn keys unique together

I'm stuck with this. I have two table like:
TABLE1 (CODE,SUBCODE,DESCR,FIELD4,FIELD5,...)

TABLE2 (ID1,ID2,CODE,SUBCODE,FIELD5,FIELD6,...)

and the model like this:
class Table1(models.Model):

    code = models.IntegerField(primary_key=True, db_column='CODE') # Field name made lowercase.

    subcode = models.IntegerField(primary_key=True, db_column='SUBCODE') # Field name made lowercase.

    description = models.CharField(db_column='DESCR', max_length=30) # Field name made lowercase.

    field4 = models.CharField(db_column='FIELD4', max_length=8) # Field name made lowercase.

    field5 = models.CharField(db_column='FIELD5', max_length=30) # Field name made lowercase.

    (...)

class Meta:

    managed = False

    db_table = 'TABLE1'

    unique_together = (("code", "subcode"),)

class Table2(models.Model):

    id1 = models.IntegerField(primary_key=True, db_column='ID1') # Field name made lowercase.

    id2 = models.IntegerField(primary_key=True, db_column='ID2') # Field name made lowercase.

    code = models.IntegerField(db_column='CODE') # Field name made lowercase.<br/><br/>
    subcode = models.IntegerField(db_column='SUBCODE') # Field name made lowercase.

    field5 = models.CharField(db_column='FIELD5', max_length=32) # Field name made lowercase.

    field6 = models.FloatField(db_column='FIELD6') # Field name made lowercase.

    code_fk = models.ForeignKey(Table1, db_column='CODE', to_field='code',       related_name='code_fk')

    subcode_fk = models.ForeignKey(Table1, db_column='SUBCODE', to_field='subcode', related_name='subcode_fk')

class Meta:

    managed = False

    db_table = 'TABLE2'

    unique_together = (("id1", "id2"),)

If I do a simple retrieve like this:
Table2.objects.all()

There's a way to show in the template for every row returned the fields code, subcode and the relative description taken from the other table?
Substantially I want to do a simple
SELECT B.CODE, B.SUBCODE, A.DESCR
FROM TABLE1 A JOIN TABLE2 B
ON A.CODE = B.CODE AND A.SUBCODE = B.SUBCODE

How do I have to modify my model to do it? 

A:

Django does not yet support multi-column primary keys... meaning only one field on your model should be marked as primary_key=True.
This presents a problem if you are not able to adapt your db schema to Django (which I'm guessing may be your case since you have managed=False)
You can try to use a module like this:
https://github.com/simone/django-compositekey
(I have not tried it personally)

