I am using the django framework to create a website where i have to do some filtering on my database. I have created a function where you can filter upon any field in the database. I need to use the __gte method to find all records which is greater than or equal to a particular field. I pass the desired field as attribute inside my function.
How can i find all records greater than or equal to any field that i pass into my function?
def top_percentiles(attribute, web_name, player_id):
#filters to find desired player e.g Ronaldo
searched_player = PlayerInfo.objects.filter(web_name=web_name, id=player_id)
#gets the value of the desired attribute e.g price = 10.0
players_attribute = getattr(searched_player[0], attribute)
#filters for all the players with a greater value e.g 10.5, 11.0
players_with_greater_attribute = PlayerInfo.objects.filter(attribute.__gte=players_attribute)
#filter for Ronaldos price, goals, assists
top_percentiles(price, "Ronaldo", 123)
top_percentiles(goals, "Ronaldo", 123)
top_percentiles(assists, "Ronaldo", 123)
Error message:
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Models.py
class PlayerInfo(models.Model):
price = models.FloatField()
assists = models.IntegerField()
bonus = models.IntegerField()
bps = models.IntegerField()
chance_of_playing_next_round = models.IntegerField()
chance_of_playing_this_round = models.IntegerField()
...
..