我有这些模特:
class Region(models.Model):
region_name = models.CharField(max_length=64)
def __str__(self):
return self.region_name
class GameRegionRelease(models.Model):
region = models.ForeignKey(
Region,
on_delete=models.CASCADE,
verbose_name='region'
)
game = models.ForeignKey(
Game,
on_delete=models.CASCADE,
verbose_name='game'
)
release_date = models.DateField(
verbose_name='release date',
default=None
)
class Game(models.Model):
game_name = models.CharField(max_length=512)
release_dates = models.ManyToManyField(
Region,
related_name='game_release_dates',
through='GameRegionRelease'
)
我希望得到的是每个游戏都列出的每个区域,即使该游戏没有区域数据。因此结果将是Game x Region,其中区域数据由GameRegionRelease填充。我想要的东西类似于以下内容:
SELECT *
FROM gameslist_region gr
CROSS JOIN gameslist_game gg
LEFT JOIN gameslist_gameregionrelease grr
ON gg.game_name = grr.game_id
AND gr.region_name = grr.region_id;
但我不知道如何使用本机ORM结构在Django中表达这一点。这可能吗?我使用的是Python 3.6.4和Django 2.0.2。
答案 0 :(得分:0)
## Get all objects of GameRegionRelease game_region_release = GameRegionRelease.objects.all() ## to access game_name game_region_release.game.game_name ## to access region_name game_region_release.region.region_name