我在参加比赛中得到了重复的回应。我只需要每场比赛的单一回应
我为此非常努力。请帮助我,因为这非常重要。我找不到为什么会发生。
型号
class JoinContestlist(models.Model):
user = models.ForeignKey(User,null=True,on_delete=models.CASCADE)
contest_no = models.CharField(max_length=100,null=True, blank=True)
match_id = models.CharField(max_length=100,null=True, blank=True)
spots = models.IntegerField(null=True, blank=True)
entry_fee = models.IntegerField(null=True, blank=True)
price_pool = models.IntegerField(null=True, blank=True)
level = models.JSONField(null=True, blank=True)
no_of_team = models.IntegerField(null=True, blank=True)
remaining_spots = models.IntegerField(null=True, blank=True)
remaining_team = models.IntegerField(null=True, blank=True)
views.py POST功能
@api_view(['POST'])
def post_join_contest_list(request, id, contest_no, match_id):
try:
if request.method == 'POST':
user = User.objects.get(id=id)
qs = Contest.objects.get(contest_no=contest_no)
cs = MatchList.objects.get(match_id=match_id)
serializer = JoinContestlistserializer(qs, data=request.data)
ks = JoinContestlist.objects.filter(user=user, contest_no=qs.contest_no, match_id=match_id)
if len(ks) < qs.no_of_team:
js = JoinContestlist.objects.create(user=user, spots=qs.spots, entry_fee=qs.entry_fee,
price_pool=qs.price_pool, level=qs.level, no_of_team=qs.no_of_team,
contest_no=qs.contest_no, match_id=cs.match_id)
js.save()
return JsonResponse({"status": True, "message": "success"})
return JsonResponse({"status": False, "message": "Contest is full"})
except:
return JsonResponse({"status": False, "message": "Service temporarily unavailable, try again later"})
获得功能
@api_view(['GET'])
def get_join_contest_list(request, id, match_id):
try:
if request.method == 'GET':
# import pdb
# pdb.set_trace()
user = User.objects.get(id=id)
cs = JoinContestlist.objects.filter(user=user, match_id=match_id)
json_data_2 = []
json_data_4 = []
json_data_1 = []
for k in cs:
qs = JoinContestlist.objects.filter(match_id=k.match_id, contest_no=k.contest_no)
ds = JoinContestlist.objects.filter(user=user, match_id=k.match_id, contest_no=k.contest_no)
a = len(qs)
b = len(ds)
remaining_spots = k.spots - a
remaining_team = k.no_of_team - b
json_data_1.append({
"contest_no": k.contest_no,
"spots": k.spots,
"remaining_spots": remaining_spots,
"entry_fee": k.entry_fee,
"prize_pool": k.price_pool,
"no_of_team": k.no_of_team,
"remaining_team": remaining_team,
"winning": [{"key": q, "value": z} for (q, z) in k.level.items()]
})
return JsonResponse(
{"status": True, "message": "success", "joined_contest": json_data_1, })
except:
return JsonResponse({"status": False, "message": "Service temporarily unavailable, try again later"})
输出我正在获得
{
"status": true,
"message": "success",
"joined_contest": [
{
"contest_no": "13",
"spots": 1118,
"remaining_spots": 1113,
"entry_fee": 99,
"prize_pool": 90759,
"no_of_team": 10,
"remaining_team": 5,
"winning": [
{
"key": "1",
"value": " 5,000 "
},
{
"key": "2",
"value": " 2,000"
},
{
"key": "3-10",
"value": "750"
},
{
"key": "11-100",
"value": "500"
},
{
"key": "101-250",
"value": "140"
},
{
"key": "251-372",
"value": "95"
}
]
},
{
"contest_no": "13",
"spots": 1118,
"remaining_spots": 1113,
"entry_fee": 99,
"prize_pool": 90759,
"no_of_team": 10,
"remaining_team": 5,
"winning": [
{
"key": "1",
"value": " 5,000 "
},
{
"key": "2",
"value": " 2,000"
},
{
"key": "3-10",
"value": "750"
},
{
"key": "11-100",
"value": "500"
},
{
"key": "101-250",
"value": "140"
},
{
"key": "251-372",
"value": "95"
}
]
},
{
"contest_no": "13",
"spots": 1118,
"remaining_spots": 1113,
"entry_fee": 99,
"prize_pool": 90759,
"no_of_team": 10,
"remaining_team": 5,
"winning": [
{
"key": "1",
"value": " 5,000 "
},
{
"key": "2",
"value": " 2,000"
},
{
"key": "3-10",
"value": "750"
},
{
"key": "11-100",
"value": "500"
},
{
"key": "101-250",
"value": "140"
},
{
"key": "251-372",
"value": "95"
}
]
},
{
"contest_no": "13",
"spots": 1118,
"remaining_spots": 1113,
"entry_fee": 99,
"prize_pool": 90759,
"no_of_team": 10,
"remaining_team": 5,
"winning": [
{
"key": "1",
"value": " 5,000 "
},
{
"key": "2",
"value": " 2,000"
},
{
"key": "3-10",
"value": "750"
},
{
"key": "11-100",
"value": "500"
},
{
"key": "101-250",
"value": "140"
},
{
"key": "251-372",
"value": "95"
}
]
},
{
"contest_no": "13",
"spots": 1118,
"remaining_spots": 1113,
"entry_fee": 99,
"prize_pool": 90759,
"no_of_team": 10,
"remaining_team": 5,
"winning": [
{
"key": "1",
"value": " 5,000 "
},
{
"key": "2",
"value": " 2,000"
},
{
"key": "3-10",
"value": "750"
},
{
"key": "11-100",
"value": "500"
},
{
"key": "101-250",
"value": "140"
},
{
"key": "251-372",
"value": "95"
}
]
},
{
"contest_no": "12",
"spots": 50,
"remaining_spots": 45,
"entry_fee": 49,
"prize_pool": 1975,
"no_of_team": 5,
"remaining_team": 0,
"winning": [
{
"key": "1-5",
"value": "125"
},
{
"key": "6-15",
"value": "75"
},
{
"key": "16-30",
"value": "40"
}
]
},
{
"contest_no": "12",
"spots": 50,
"remaining_spots": 45,
"entry_fee": 49,
"prize_pool": 1975,
"no_of_team": 5,
"remaining_team": 0,
"winning": [
{
"key": "1-5",
"value": "125"
},
{
"key": "6-15",
"value": "75"
},
{
"key": "16-30",
"value": "40"
}
]
},
{
"contest_no": "12",
"spots": 50,
"remaining_spots": 45,
"entry_fee": 49,
"prize_pool": 1975,
"no_of_team": 5,
"remaining_team": 0,
"winning": [
{
"key": "1-5",
"value": "125"
},
{
"key": "6-15",
"value": "75"
},
{
"key": "16-30",
"value": "40"
}
]
},
{
"contest_no": "12",
"spots": 50,
"remaining_spots": 45,
"entry_fee": 49,
"prize_pool": 1975,
"no_of_team": 5,
"remaining_team": 0,
"winning": [
{
"key": "1-5",
"value": "125"
},
{
"key": "6-15",
"value": "75"
},
{
"key": "16-30",
"value": "40"
}
]
},
{
"contest_no": "12",
"spots": 50,
"remaining_spots": 45,
"entry_fee": 49,
"prize_pool": 1975,
"no_of_team": 5,
"remaining_team": 0,
"winning": [
{
"key": "1-5",
"value": "125"
},
{
"key": "6-15",
"value": "75"
},
{
"key": "16-30",
"value": "40"
}
]
}
]
}
我在加入比赛中获得了重复的响应。我只需要每场比赛
。
{
"status": true,
"message": "success",
"joined_contest": [
{
"contest_no": "13",
"spots": 1118,
"remaining_spots": 1113,
"entry_fee": 99,
"prize_pool": 90759,
"no_of_team": 10,
"remaining_team": 5,
"winning": [
{
"key": "1",
"value": " 5,000 "
},
{
"key": "2",
"value": " 2,000"
},
{
"key": "3-10",
"value": "750"
},
{
"key": "11-100",
"value": "500"
},
{
"key": "101-250",
"value": "140"
},
{
"key": "251-372",
"value": "95"
}
]
},
{
"contest_no": "12",
"spots": 50,
"remaining_spots": 45,
"entry_fee": 49,
"prize_pool": 1975,
"no_of_team": 5,
"remaining_team": 0,
"winning": [
{
"key": "1-5",
"value": "125"
},
{
"key": "6-15",
"value": "75"
},
{
"key": "16-30",
"value": "40"
}
]
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在查询上尝试过
DISTICT()
?Have you tried
distinct()
on your query?https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.query.QuerySet.distinct