首页 > 解决方案 > Django - 如何在 if 语句中终止 content_type?

问题描述

我想在我的函数中找出我的内容类型,但无论我如何设计我的 if 语句,我都无法达到“让我”打印语句。我还尝试使用 pprint 来获取 content_type 的命名,但直到现在都没有效果。

def mark_refunded(modeladmin, request, queryset):
    queryset.update(status=2)
    transactions = queryset.all()
    for transaction in queryset.all():
        print(transaction)
        transaction.sender.acc_usd_balance += float(transaction.amount)
        transaction.sender.save()
        transaction.receiver.acc_usd_balance -= float(transaction.amount)
        transaction.receiver.save()
        print(transaction.content_type)
        if transaction.content_type == "App | Sell Type A":
            print("got me")

似乎我无法将 print(transaction.content_type) 的输出与 if 语句进行比较,为什么呢?我希望输出与我在 if 语句中要求的值相同。

标签: djangocontent-type

解决方案


假设content_type是外键,您需要比较if str(...) == ....

然而,这里更重要的问题是多个并发请求最终肯定会破坏这些帐户余额,因为您没有以原子方式管理它们。


推荐阅读