Home > Mobile >  Django filter column with OR statement
Django filter column with OR statement

Time:12-18

I found a question very similar to this one but that one did not specify how to return a specific column and I have been trying combinations for the past few hours with no luck.

home_team_list2 = PreviousLossesNbav1WithDateAgg.objects.values_list('actual_over_under_result_field', flat=True).filter(Q(away_team_field="Chicago") | Q(home_team_field="Chicago"))

This does not throw me any errors but it does not return anything. I am attempting to return that specific column from my models filtering on away team or home team equal to "Chicago"

Here is the model I am attempting to pull from:

class PreviousLossesNbav1WithDateAgg(models.Model):
    home_team_field = models.TextField(db_column='HOME TEAM:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_team_field = models.TextField(db_column='AWAY TEAM:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    projected_points_field = models.FloatField(db_column='PROJECTED POINTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_injury = models.TextField(db_column='Home Injury', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    away_injury = models.TextField(db_column='Away Injury', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    game_points_with_formula_field = models.BigIntegerField(db_column='GAME POINTS WITH FORMULA:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    game_money_line_field = models.FloatField(db_column='GAME MONEY LINE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    over_or_under = models.TextField(db_column='OVER OR UNDER', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters.
    actual_game_points_field = models.BigIntegerField(db_column='ACTUAL GAME POINTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_team_points_field = models.BigIntegerField(db_column='AWAY TEAM POINTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_team_points_field = models.BigIntegerField(db_column='HOME TEAM POINTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    actual_over_under_result_field = models.TextField(db_column='ACTUAL OVER UNDER RESULT:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    percentage_won_field = models.FloatField(db_column='Percentage Won:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    win_or_lose_field = models.TextField(db_column='Win Or Lose:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_assists_field = models.BigIntegerField(db_column='AWAY ASSISTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_blocks_field = models.BigIntegerField(db_column='AWAY BLOCKS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_defensive_rebounds_per_game_field = models.BigIntegerField(db_column='AWAY DEFENSiVE REBOUNDS PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_effective_field_goal_field = models.FloatField(db_column='AWAY EFFECTIVE FIELD GOAL %:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_fg_field = models.FloatField(db_column='AWAY FG %:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_fg_attempts_field = models.BigIntegerField(db_column='AWAY FG ATTEMPTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_fico_field = models.FloatField(db_column='AWAY FICO:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_free_throw_field = models.FloatField(db_column='AWAY FREE THROW %:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_free_throw_rate_field = models.FloatField(db_column='AWAY FREE THROW RATE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_offensive_rebounds_per_game_field = models.BigIntegerField(db_column='AWAY OFFENSIVE REBOUNDS PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_personal_fouls_field = models.BigIntegerField(db_column='AWAY PERSONAL FOULS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_points_per_game_field = models.FloatField(db_column='AWAY POINTS PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_steal_percentage_field = models.FloatField(db_column='AWAY STEAL PERCENTAGE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_team_3_field = models.FloatField(db_column='AWAY TEAM 3%:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_team_opp_ppg_field = models.FloatField(db_column='AWAY TEAM OPP PPG:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_team_pace_field = models.FloatField(db_column='AWAY TEAM PACE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_team_projected_points_field = models.FloatField(db_column='AWAY TEAM PROJECTED POINTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_total_shot_percentage_field = models.FloatField(db_column='AWAY TOTAL SHOT PERCENTAGE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_turnover_per_game_field = models.BigIntegerField(db_column='AWAY TURNOVER PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    away_turnover_percentage_field = models.FloatField(db_column='AWAY TURNOVER PERCENTAGE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_assists_field = models.BigIntegerField(db_column='HOME ASSISTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_blocks_field = models.BigIntegerField(db_column='HOME BLOCKS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_defensive_rebounds_per_game_field = models.BigIntegerField(db_column='HOME DEFENSiVE REBOUNDS PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_effective_field_goal_field = models.FloatField(db_column='HOME EFFECTIVE FIELD GOAL %:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_fg_field = models.FloatField(db_column='HOME FG %:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_fg_attempts_field = models.BigIntegerField(db_column='HOME FG ATTEMPTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_fico_field = models.FloatField(db_column='HOME FICO:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_free_throw_field = models.FloatField(db_column='HOME FREE THROW %:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_free_throw_rate_field = models.FloatField(db_column='HOME FREE THROW RATE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_offensive_rebounds_per_game_field = models.BigIntegerField(db_column='HOME OFFENSIVE REBOUNDS PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_personal_fouls_field = models.BigIntegerField(db_column='HOME PERSONAL FOULS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_points_per_game_field = models.FloatField(db_column='HOME POINTS PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_steal_percentage_field = models.FloatField(db_column='HOME STEAL PERCENTAGE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_team_3_field = models.FloatField(db_column='HOME TEAM 3%:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_team_opp_ppg_field = models.FloatField(db_column='HOME TEAM OPP PPG:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_team_pace_field = models.FloatField(db_column='HOME TEAM PACE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_team_projected_points_field = models.FloatField(db_column='HOME TEAM PROJECTED POINTS:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_total_shot_percentage_field = models.FloatField(db_column='HOME TOTAL SHOT PERCENTAGE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_turnover_per_game_field = models.BigIntegerField(db_column='HOME TURNOVER PER GAME:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    home_turnover_percentage_field = models.FloatField(db_column='HOME TURNOVER PERCENTAGE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
    date_field = models.DateTimeField(db_column='DATE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.

    class Meta:
        managed = False
        db_table = 'previous_losses_nbav1_with_date_agg'

CodePudding user response:

You just need to put filter first and then values_list. This should work.

home_team_list2 = PreviousLossesNbav1WithDateAgg.objects.filter(
Q(away_team_field="Chicago") | Q(home_team_field="Chicago")
).values_list('actual_over_under_result_field', flat=True)

I tried this on my local machine and it worked fine.

SELECT `previous_losses_nbav1_with_date_agg`.`ACTUAL OVER UNDER RESULT:` 
FROM `previous_losses_nbav1_with_date_agg` WHERE 
(`previous_losses_nbav1_with_date_agg`.`AWAY TEAM:` = Chicago OR 
`previous_losses_nbav1_with_date_agg`.`HOME TEAM:` = Chicago)

This generated output of query is not valid SQL because "Django never actually interpolates the parameters: it sends the query and the parameters separately to the database adapter, which performs the appropriate operations." Source: enter image description here

enter image description here

  • Related