尽管 dict.copy() 从原始字典中删除了键

发布于 2025-01-11 21:46:45 字数 1601 浏览 0 评论 0原文

我最近正在尝试使用会话变量,而不是在基础中编写所有内容。 我正在构建一个用于柜台销售的应用程序(购物车、用户、客户会话变量)。 将订单及其项目保存在数据库中后,我想保存客户并清除 request.session['customers'] 字典列表中的相关会话变量。

def save_customer(request, store):
    # save only customers != Client Magasin
    customers = request.session['customers'].copy()
    print(customers)
    current_customer = list(filter(
       lambda i: i['user'] == request.user.pk and i['store'] == store, customers))[0]
    id = current_customer.pop('id')
    nom = current_customer['nom']
    if nom != 'Client Magasin':
        current_customer.pop('store')
        current_customer.pop('user')
        customer = Customer.objects.create(**current_customer)
        # return Customer for adding it to order
        return customer
    else:
        # return Client Magasin
        return Customer.objects.get(pk=1)

当我打印出 request.session['customers'] 时,我可以看到 ID、STORE 和 USER 键。但是在 lambda 之后,这些键将从原始 request.session['customers'] 中删除

,我知道因为在我的代码中,我不想再次过滤这个列表,

def remove_same_customer(request):
    customers_list = request.session['customers'].copy()
    print(customers_list)
    # new_customers = list(filter(
    #     lambda i:  i['store'] != request.session['cart_warehouse'] and i['user'] != 
        request.user, customers_list))
    # request.session['customers'] = new_customers
    # request.session.modified = True

我已经评论了 4最后几行,因为它们会触发错误,因为无法找到 ID、STORE 和 USER 键。这是通过使用 print(customers_list) 确认的,

我做错了什么? PS:不知道 dict.copy() 在我的情况下是否有用,但我在引发错误后尝试了这个。

I'm recently experimenting with session variables instead of writing everything in base.
I'm building an app for counter sales (cart, user, customer session variables).
After the order and its items are saved in DB, I want to save the customer and clear its related session variablesin request.session['customers'] dict list.

def save_customer(request, store):
    # save only customers != Client Magasin
    customers = request.session['customers'].copy()
    print(customers)
    current_customer = list(filter(
       lambda i: i['user'] == request.user.pk and i['store'] == store, customers))[0]
    id = current_customer.pop('id')
    nom = current_customer['nom']
    if nom != 'Client Magasin':
        current_customer.pop('store')
        current_customer.pop('user')
        customer = Customer.objects.create(**current_customer)
        # return Customer for adding it to order
        return customer
    else:
        # return Client Magasin
        return Customer.objects.get(pk=1)

When I print out request.session['customers'] I can see ID, STORE and USER keys. But after the lambda, those keys are removed from the original request.session['customers']

I know that because further in my code, I wan't to filter again this list

def remove_same_customer(request):
    customers_list = request.session['customers'].copy()
    print(customers_list)
    # new_customers = list(filter(
    #     lambda i:  i['store'] != request.session['cart_warehouse'] and i['user'] != 
        request.user, customers_list))
    # request.session['customers'] = new_customers
    # request.session.modified = True

I have commented the 4 last lines because they trigger an error because ID, STORE and USER keys can't be found. This is confirmed by using print(customers_list)

what am I doing wrong ?
PS : dunno if dict.copy() is useful in my case but I tried this after the error raised.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文