使用select_related()的Django过滤器

时间:2016-12-20 22:08:40

标签: sql django django-models

我有以下完美的查询:

    campaignFixtures = UserSelection.objects.select_related().filter(user=currentUserID,campaignno=currentCampaignNo).order_by('fixtureid__fixturedate')[:1]

但是,我需要按如下方式过滤另一个表中的字段:

    campaignFixtures = UserSelection.objects.select_related().filter(user=currentUserID,campaignno=currentCampaignNo,straightredfixture__fixturematchday=18).order_by('fixtureid__fixturedate')[:1]

但是,我收到以下错误:

Cannot resolve keyword 'straightredfixture' into field. Choices are: campaignno, fixtureid, fixtureid_id, teamselection1or2, teamselectionid, teamselectionid_id, user, user_id, userselectionid

模型如下:

class StraightredFixture(models.Model):
    fixtureid = models.IntegerField(primary_key=True)
    home_team = models.ForeignKey('straightred.StraightredTeam', db_column='hometeamid', related_name='home_fixtures')
    away_team = models.ForeignKey('straightred.StraightredTeam', db_column='awayteamid', related_name='away_fixtures')
    fixturedate = models.DateTimeField(null=True)
    fixturestatus = models.CharField(max_length=24,null=True)
    fixturematchday = models.ForeignKey('straightred.StraightredFixtureMatchday', db_column='fixturematchday')
    spectators = models.IntegerField(null=True)
    hometeamscore = models.IntegerField(null=True)
    awayteamscore = models.IntegerField(null=True)
    homegoaldetails = models.TextField(null=True)
    awaygoaldetails = models.TextField(null=True)
    hometeamyellowcarddetails = models.TextField(null=True)
    awayteamyellowcarddetails = models.TextField(null=True)
    hometeamredcarddetails = models.TextField(null=True)
    awayteamredcarddetails = models.TextField(null=True)
    soccerseason = models.ForeignKey('straightred.StraightredSeason', db_column='soccerseasonid', related_name='fixture_season')


    def __unicode__(self):
        return self.fixtureid

    class Meta:
        managed = True
        db_table = 'straightred_fixture'

class UserSelection(models.Model):
    userselectionid = models.AutoField(primary_key=True)
    campaignno = models.CharField(max_length=36,unique=False)
    user = models.ForeignKey(User, related_name='selectionUser')
    teamselection1or2 = models.PositiveSmallIntegerField()
    teamselectionid = models.ForeignKey('straightred.StraightredTeam', db_column='teamselectionid', related_name='teamID')
    fixtureid = models.ForeignKey('straightred.StraightredFixture', db_column='fixtureid')


    class Meta:
        managed = True
        db_table = 'straightred_userselection'

任何帮助将不胜感激,艾伦。

2 个答案:

答案 0 :(得分:3)

我不认为问题与selected_related有关。您只是尝试使用错误的查找值进行过滤。如何使用fixtureid__fixturematchday进行过滤:

UserSelection.objects.select_related().filter(user=currentUserID, campaignno=currentCampaignNo, fixtureid__fixturematchday=18).order_by('fixtureid__fixturedate')[:1]

由于您只想获得一个对象,为什么不使用.first()来获取对象而不是带有一个项目的查询集:

campaignFixture = UserSelection.objects.select_related("fixtureid").filter(...).order_by(...).first()

答案 1 :(得分:2)

根据你的模型,关系是夹具

 UserSelection.objects.select_related().filter(user=currentUserID,campaignno=currentCampaignNo,fixtureid__fixturematchday=18)