我如何接收 djano 中的词典列表?

发布于 2025-01-09 23:37:27 字数 663 浏览 3 评论 0原文

你好,我正在开发一个 djago 项目,我想收到一个字典列表,如下所示: [{1:20}、{2:30}、{3:50}、......] 这里的key是产品的idvalueprice 下面的代码只是接收一个像这样的单个字典 {“id”:1,“价格”:20} 我想将其更改为我上面提到的样子,

 list_of_objects = []
        try:
            id = int(request.payload.get("id"))
            price = int(request.payload.get("price"))
            list_of_objects.append({
                "id" : id,
                "price" : price
            })
        except ValueError:
            return response.bad_request("Invalid price, %s, should be in whole dollars" % price)



但我不知道该怎么做 谢谢

Hello I am working on a djago project and I want to recieve a list of dictionaries just like this:
[{1: 20}, {2:30}, {3: 50}, ......]
here the key is the id of the productand value is price
And the code below is just receiving a single dictionary like this
{"id": 1, "price" : 20}
I want to change it to something look like I mentioned above

 list_of_objects = []
        try:
            id = int(request.payload.get("id"))
            price = int(request.payload.get("price"))
            list_of_objects.append({
                "id" : id,
                "price" : price
            })
        except ValueError:
            return response.bad_request("Invalid price, %s, should be in whole dollars" % price)



I don't know how to do it
Thanks

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

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

发布评论

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

评论(3

坏尐絯℡ 2025-01-16 23:37:27

试试这个

        list_of_objects = []
        try:
            id = int(request.payload.get("id"))
            price = int(request.payload.get("price"))
            list_of_objects.append({
                id: price   # here changes
            })
        except ValueError:
            return response.bad_request("Invalid price, %s, should be in whole dollars" % price)

Try this

        list_of_objects = []
        try:
            id = int(request.payload.get("id"))
            price = int(request.payload.get("price"))
            list_of_objects.append({
                id: price   # here changes
            })
        except ValueError:
            return response.bad_request("Invalid price, %s, should be in whole dollars" % price)

时间海 2025-01-16 23:37:27

这应该可以解决问题:

    your_dict = {}
    try:
        id = int(request.payload.get("id"))
        price = int(request.payload.get("price"))
        your_dict[id] = price
    except ValueError:
        return response.bad_request("Invalid price, %s, should be in whole dollars" % price)

This should do the trick:

    your_dict = {}
    try:
        id = int(request.payload.get("id"))
        price = int(request.payload.get("price"))
        your_dict[id] = price
    except ValueError:
        return response.bad_request("Invalid price, %s, should be in whole dollars" % price)
成熟的代价 2025-01-16 23:37:27

这对我有用;不久前我也做了同样的事情。

request_dict = dict(request.payload)

我希望它也适合你。

This worked for me; I did the same thing a little while ago.

request_dict = dict(request.payload)

I hope it works for you too.

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