我在参加比赛中得到了重复的回应。我只需要每场比赛的单一回应

发布于 2025-02-02 00:17:12 字数 12961 浏览 1 评论 0 原文

我为此非常努力。请帮助我,因为这非常重要。我找不到为什么会发生。

型号

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"
                }
            ]
        }
    ]
}

i have worked very hard on this.Please help me out as this is very important. I am not able to find why is it happening.

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 Function

@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"})

GET Function

@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 i am getting

{
    "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"
                }
            ]
        }
    ]
}

I am getting duplicate responses in joined contest. I want only single response for every contest.Below is my expected output

EXPECTED 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"
                }
            ]
        }
    ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2025-02-09 00:17:12

您是否在查询上尝试过 DISTICT()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文