Basically it refuses to give me ForeignKey object by its index. Any ideas?
Unless you specify an order, a database can return the records in any possible order. Hence that means that making the same (unordered) query multiple times, can each time yield a different result.
It is therefore advisable to use .order_by() [Django-doc] if something should have a deterministic order. For example in case of pagination, not doing so, can break the pagintion.
Here you thus can inspect the query with:
Match.objects.order_by('pk')[0].team_set.order_by('pk')
Match.objects.order_by('pk')[0].team_set.order_by('pk')[0]
Match.objects.order_by('pk')[0].team_set.order_by('pk')[0:1]
Match.objects.order_by('pk')[0].team_set.order_by('pk')[1]
Match.objects.order_by('pk')[0].team_set.order_by('pk')[1:2]
That being said, accessing objects by id, is not very efficient: this will make *a query to the database per index. It is therefore better to aim to "batch" the queries, and thus make one query, that fetches for example two records.
For example you can obtain the first two records and perform iterable unpacking:
team1, team2 = Match.objects.order_by('pk')[0].team_set.order_by('pk')[:2]
Here team1 is the first team, and team2 is the second. This will raise an error if there are no two teams (but since it is a Match this is likely not the case). You can then pass the two teams for example to the template.
team_set.order_by('pk')[0]for example, then you set the order, and thus then this shoud not happen.MatchobjectMatch.objects.all()[0]is different as well.