The function not entering into if condition, because round is dict and match_round is int, so showing error, how solve this?
my modelviewset
def destroy(self, request, *args, **kwargs):
gameevent = self.get_object().gameevent
match_round = self.get_object().match_round
print(gameevent)
print(match_round)
round = Matchscore.objects.filter(gameevent=gameevent, match_round=match_round).values_list("match_round",flat=True).aggregate(Max("match_round"))
print(round)
if round == match_round:
Matchscore.objects.get(match_round=round).delete()
response = {"result": "successfully removed"}
else:
response = {"result": "can't delete this round"}
return Response(response)
CodePudding user response:
try this,
def destroy(self, request, *args, **kwargs):
gameevent = self.get_object().gameevent
match_round = self.get_object().match_round
print(gameevent)
print(match_round)
round = Matchscore.objects.filter(gameevent=gameevent, match_round=match_round).values_list("match_round",flat=True).aggregate(Max("match_round"))['match_round__max']
print(round)
if round == match_round:
Matchscore.objects.get(match_round=round).delete()
response = {"result": "successfully removed"}
else:
response = {"result": "can't delete this round"}
return Response(response)
CodePudding user response:
If you want to compare Django DB objects for identity in the DB, i.e. sharing the same DB row, then test their pk for equality.
if a_round.pk == match_round.pk:
if by some other metric based on other fields, code this explicitly.
In the question, round
is not a Django DB object at all, it's a values_list
based on some filtered objects. So you need to extract some values from the result of that query, and the corresponding values from match_round
, and compare them. I don't have sufficient insight into the problem to suggest the nature of that comparison.