Alan Tingey
Alan Tingey

Reputation: 971

Django - How to link tables

Hello to the stackoverflow team,

I have the following two django tables:

class StraightredFixture(models.Model):
    fixtureid = models.IntegerField(primary_key=True)
    soccerseason = models.IntegerField(db_column='soccerSeason')  # Field name made lowercase.
    hometeamid = models.IntegerField()
    awayteamid = models.IntegerField()
    fixturedate = models.DateTimeField()
    fixturestatus = models.CharField(max_length=24)
    fixturematchday = models.IntegerField()
    hometeamscore = models.IntegerField()
    awayteamscore = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'straightred_fixture'

class StraightredTeam(models.Model):
    teamid = models.IntegerField(primary_key=True)
    teamname = models.CharField(max_length=36)
    teamcode = models.CharField(max_length=5)
    teamshortname = models.CharField(max_length=24)

    class Meta:
        managed = False
        db_table = 'straightred_team'

In the views.py I know I can put the following and it works perfectly:

def test(request):
    fixture = StraightredFixture.objects.get(fixtureid=136697)
    return render(request,'straightred/test.html',{'name':fixture.hometeamid})

As I mentioned above, this all works well but I am looking to return the teamname of the hometeamid which can be found in the StraightredTeam model.

After some looking around I have been nudged in the direction of "select_related" but I am unclear on how to implement it in my existing tables and also if it is the most efficient way for this type of query. It feels right.

Please note this code was created using "python manage.py inspectdb".

Any advice at this stage would be greatly appreciated. Many thanks, Alan.

Upvotes: 1

Views: 355

Answers (2)

Tobia Tesan
Tobia Tesan

Reputation: 1936

I am not sure if this makes sense for Managed=False, but I suppose the sane way of doing it in Django would be with

home_team = models.ForeignKey('StraightRedFixture', db_column='fixtureid'))

And then just using fixture.home_team instead of doing queries by hand.

Upvotes: 1

aumo
aumo

Reputation: 5574

See model relationships.

Django provides special model fields to manage table relationships. The one suiting your needs is ForeignKey.

Instead of declaring:

hometeamid = models.IntegerField()
awayteamid = models.IntegerField()

which I guess is the result of python manage.py inspectdb, you would declare:

home_team = models.ForeignKey('<app_name>. StraightredTeam', db_column='hometeamid', related_name='home_fixtures')
away_team = models.ForeignKey('<app_name>. StraightredTeam', db_column='awayteamid', related_name='away_fixtures')

By doing this will, you tell the Django ORM to handle the relationship under the hood, which will allow you to do such things as:

fixture = StraightredFixture.objects.get(fixtureid=some_fixture_id)
fixture.home_team  # Returns the associated StraightredTeam instance.

team = StraightredTeam.objects.get(team_id=some_team_id)
team.home_fixtures.all()  # Return all at home fixtures for that team.

Upvotes: 1

Related Questions