【问题标题】:'float' object is not iterable in django queryset'float' 对象在 Django 查询集中不可迭代
【发布时间】:2022-01-14 13:53:30
【问题描述】:
class TransactionHistoryListSerializer(serializers.BaseSerializer):
    class Meta:
        model = CustomerVisit

    def to_representation(self, instance):

        price = 0
        package_price = booking_models.TestBooking.objects.filter(customer_visit=instance).values_list(
            "package__test__test_mrp", flat=True
        )[0]
        for price in package_price:
            pass

        return {
            "visit_id": instance.id,
            "customer_name": {
                "salutation": instance.customer.salutation,
                "first_name": instance.customer.first_name,
                "middle_name": instance.customer.middle_name,
                "last_name": instance.customer.last_name,
            },
            "dob": instance.customer.date_of_birth,
            "amount": sum([k.amount for k in booking_models.TestBooking.objects.filter(customer_visit=instance)])
            + (price if price else 0),
            "discount": sum([k.discount for k in booking_models.TestBooking.objects.filter(customer_visit=instance)]),
            "paid_amount": sum([k.amount for k in payment_models.Payment.objects.filter(customer_visit=instance)]),
        }

这里 tes_mrp 是浮点型。 我搜索并获得了使用列表理解的解决方案,但仍然无法正常工作。我试过[[price] for price in package_price]for price in package_price: a = list(str(price)),但仍然得到浮动对象是不可迭代的。我肯定做错了什么。有人可以帮忙吗。谢谢!!

【问题讨论】:

  • 您应该显示回溯到出现错误的行。您的代码中有很多地方可以做到这一点。
  • 我的错,我会从下一次开始处理它。这导致了这一行 - 对于 package_price 中的价格:

标签: python django django-rest-framework


【解决方案1】:

这是导致这一行的原因 - 对于 package_price 中的价格:

好吧,那是因为你在做.values_list(..., flat=True),它返回一个列表,然后用[0] 对其进行索引,所以你得到了第一个值。 package_price 因此确实是一个浮点数,正如错误所说,您不能迭代单个浮点数。

因此,只需取出 [0] 即可修复该特定位。

您的代码可以简化为不重复查询并使用 Django 的聚合:

def to_representation(self, instance):
    bookings = booking_models.TestBooking.objects.filter(customer_visit=instance)
    payments = payment_models.Payment.objects.filter(customer_visit=instance)

    package_price = bookings.values_list("package__test__test_mrp", flat=True).first()
    booking_amounts = bookings.aggregate(
        price_sum=Sum("amount"),
        discount_sum=Sum("discount"),
    )
    payment_amounts = payments.aggregate(
        payment_sum=Sum("amount"),
    )

    return {
        "visit_id": instance.id,
        "customer_name": {
            "salutation": instance.customer.salutation,
            "first_name": instance.customer.first_name,
            "middle_name": instance.customer.middle_name,
            "last_name": instance.customer.last_name,
        },
        "dob": instance.customer.date_of_birth,
        "amount": (booking_amounts.get("price_sum") or 0) + package_price,
        "discount": booking_amounts.get("discount_sum") or 0,
        "paid_amount": payment_amounts.get("payment_sum") or 0,
    }

【讨论】:

  • 感谢您的回答,但现在我收到此错误 - 此行中的浮动对象不可下标 - package_price = package_price_record[0] if package_price_record else 0
  • 我的错,现在修好了。
  • 是的,它正在工作,但您能否再确认一件事。每次迭代时 - 它是否采用 test_mrp [0]。因为这是我必须做的,因为测试是 m2m 字段。
  • 不保证m2m字段的迭代顺序,所以你会得到一些测试对象。行为与[0] 相同。
猜你喜欢
  • 2023-04-06
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 2019-10-09
相关资源
最近更新 更多