Я получаю дублирующиеся ответы в объединенных конкурсах. Я хочу получить только один ответ для каждого конкурса
Я очень много работал над этим. Пожалуйста, помогите мне, так как это очень важно. Я не могу найти, почему это происходит.
models.py
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"})
OUTPUT я получаю
Я получаю дубликаты ответов в присоединенных конкурсах. Я хочу получить только один ответ для каждого конкурса.Ниже приведен ожидаемый результат
ОЖИДАЕМЫЙ РЕЗУЛЬТАТ
{
"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"
}
]
}
]
}
Вы пробовали distinct()
на своем запросе?
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.query.QuerySet.distinct