int() 参数必须是字符串、类字节对象或数字,而不是 'dict'
int() argument must be a string, a bytes-like object or a number, not 'dict'
函数没有进入if条件,因为round是dict,match_round是int,所以报错,怎么解决?
我的模型视图集
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)
试试这个,
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)
如果您想比较 Django 数据库对象在数据库中的身份,即共享相同的数据库行,则测试它们的 pk 是否相等。
if a_round.pk == match_round.pk:
如果基于其他字段的某些其他指标,请明确编码。
在问题中,round
根本不是 Django DB 对象,它是基于一些过滤对象的 values_list
。因此,您需要从该查询的结果中提取一些值,并从 match_round
中提取相应的值,然后比较它们。我对这个问题没有足够的洞察力来暗示这种比较的性质。
函数没有进入if条件,因为round是dict,match_round是int,所以报错,怎么解决?
我的模型视图集
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)
试试这个,
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)
如果您想比较 Django 数据库对象在数据库中的身份,即共享相同的数据库行,则测试它们的 pk 是否相等。
if a_round.pk == match_round.pk:
如果基于其他字段的某些其他指标,请明确编码。
在问题中,round
根本不是 Django DB 对象,它是基于一些过滤对象的 values_list
。因此,您需要从该查询的结果中提取一些值,并从 match_round
中提取相应的值,然后比较它们。我对这个问题没有足够的洞察力来暗示这种比较的性质。