1๐
โ
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.
๐คaumo
1๐
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.
๐คTobia Tesan
- [Answered ]-How do I make an html div element follow vertical scrolling (in html/django)?
- [Answered ]-Django installation error
- [Answered ]-Manager isn't accessible via SocialToken instances
- [Answered ]-Fixing request entity too large error under django/apache/mod_wsgi for file uploads
- [Answered ]-Getting how the view was called with HttpRedirect in the next view
Source:stackexchange.com