类型错误:'decimal.Decimal'实例和'dict'之间不支持'小于等于'运算符

3

我不确定如何解决这个问题。我尝试将highest_bid转换为float:

highest_bid = float(Bid.objects.filter(id=bid_item.id).aggregate(Max('bid_input')))

但是这也产生了一个错误,因为字典不能以这种方式转换为float。所以我不确定如何解决这个问题。我正在尝试构建一个拍卖网站,并希望如果用户提交的出价低于清单起始价格并且低于所有已提交出价的最高出价,则拒绝其提交的出价。

错误

Internal Server Error: /listing/2/bid
Traceback (most recent call last):
  File "C:\Python\Python385\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Python\Python385\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\sarah\Desktop\commerce\auctions\views.py", line 157, in placebid
    if bidform.cleaned_data['bid_input'] < listing.start_price and bidform.cleaned_data['bid_input'] <= highest_bid:
TypeError: '<=' not supported between instances of 'decimal.Decimal' and 'dict'
[08/Oct/2020 13:59:48] "POST /listing/2/bid HTTP/1.1" 500 62598

views.py

def placebid(request, id):
    listing_bid = get_object_or_404(Listing, id=id)
    highest_bid = Bid.objects.filter(id=id).aggregate(Max('bid_input'))
    listing = Listing.objects.get(pk=id)
    if request.method == "POST":
        bidform = BidForm(request.POST)
        if bidform.is_valid():
            if bidform.cleaned_data['bid_input'] < listing.start_price and bidform.cleaned_data['bid_input'] <= highest_bid:
                return render(request, "auctions/listingPage.html", {
                    "listing": listing,
                    "error": "Make sure your bid is greater than the start price and current highest bid"
                    })
            else:
                listing_bid.bid_input = bidform.cleaned_data['bid_input']
                listing_bid.bidder = request.user
                listing_bid.bid_item = listing
                listing_bid.time = timezone.now()
                listing_bid.save()
                messages.success(request, 'Bid placed succesfully')
                return HttpResponseRedirect(reverse("listing", args=(id,)))
        else:
            return render(request, "auctions/listingPage.html", {
                "bidform": bidform })
    else:
        bidform = BidForm()
    return render(request, "auctions/listingPage.html", {
        "bidform": bidform })

请告知我是否需要提供更多详细信息。
编辑后附加:models.py。
class Listing(models.Model):

    class NewManager(models.Manager):
        def get_queryset(self):
            return super().get_queryset().filter(status='active')

    options = (
        ('active', 'Active'),
        ('closed', 'Closed'),
    )

    title = models.CharField(max_length=64)
    description = models.TextField(max_length=64)
    start_price = models.DecimalField(max_digits=9, decimal_places=2, validators=[MinValueValidator(0.99)])
    image = models.URLField(max_length=200, blank=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="listings")
    lister = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=None, null=True, blank=True, related_name="lister_user")
    date_added = models.DateTimeField(default=timezone.now)
    status = models.CharField(max_length=10, choices=options, default="active")
    winner = models.ForeignKey('Bid', on_delete=models.CASCADE, null=True, related_name="bid_winner")
    favourites = models.ManyToManyField(User, related_name="favourite", default=None, blank=True)
    objects = models.Manager()
    listingmanager = NewManager()

    def __str__(self): 
        return f"{self.title} ({self.pk}, £{self.start_price}, {self.lister})"


class Bid(models.Model):
    bidder = models.ForeignKey(User, on_delete=models.CASCADE, related_name="bidders")
    bid_item = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="bid_items", default=None)
    bid_input = models.DecimalField(max_digits=9, decimal_places=2, default=None)
    time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return f"{self.bidder}, bid amount: {self.bid_input}"

编辑附加:更新了views.py文件

def placebid(request, id):
    listing_bid = get_object_or_404(Listing, id=id)
    highest_bid = Bid.objects.filter(bid_item_id=id).aggregate(Max('bid_input'))['bid_input__max'] or Decimal('0')
    listing = Listing.objects.get(pk=id)
    if request.method == "POST":
        bidform = BidForm(request.POST)
        if bidform.is_valid():
            if bidform.cleaned_data['bid_input'] < listing.start_price and bidform.cleaned_data['bid_input'] <= highest_bid:
                return render(request, "auctions/listingPage.html", {
                    "listing": listing,
                    "error": "Make sure your bid is greater than the start price and current highest bid"
                })
            else:
                newbid = bidform.save(commit=False)
                newbid.bidder = request.user
                newbid.bid_input = bidform.cleaned_data['bid_input']
                newbid.bid_item = listing_bid
                newbid.time = timezone.now()
                newbid.save()
                messages.success(request, 'Bid placed succesfully')
            return HttpResponseRedirect(reverse("listing", args=(id,)))
        else:
            return render(request, "auctions/listingPage.html", {
                "bidform": bidform })
    else:
        bidform = BidForm()
    return render(request, "auctions/listingPage.html", {
        "bidform": bidform })
1个回答

2

highest_bid 是一个字典,将聚合名称映射到一个值,例如 { 'bid_input__max': Decimal('14.25') },因此您可以通过下标操作从字典中获取值:

highest_bid = Bid.objects.filter(
    id=id
).aggregate(Max('bid_input'))<b>['bid_input__max']</b>

但是很可能这不会给你预期的值。你过滤的是id=id,所以查询集跨越了一个记录。你可能想要在bid_item_id=id或类似的东西上进行过滤:

highest_bid = Bid.objects.filter(
    <b>bid_item_id=id</b>
).aggregate(Max('bid_input'))['bid_input__max']

根据您的评论,可能还没有出价。在这种情况下,它将返回一个None。我们可以使用以下方法来代替:

highest_bid = Bid.objects.filter(
    bid_item_id=id
).aggregate(Max('bid_input'))['bid_input__max'] <b> or Decimal('0')</b>

谢谢,我先尝试了您的第二个建议,但出现了错误:FieldError at /listing/2/bid Cannot resolve keyword 'listing_id' into field. Choices are: bid_input, bid_item, bid_item_id, bid_winner, bidder, bidder_id, id, time。当我将其保留为id=id时,错误会出现为:'<=' not supported between instances of 'decimal.Decimal' and 'NoneType' - sarchi-xo
@sarchi-xo:BidListing有什么关系?你能分享相关的模型吗? - Willem Van Onsem
@sarchi-xo:但是你在结尾处添加了 ['bid_input__max'] 吗? - Willem Van Onsem
@sarchi-xo:None类型表示在那个时刻没有出价。您可以使用... or ...表达式将其替换为零。请参见编辑。 - Willem Van Onsem
非常感谢您花费时间和耐心与我交流,问题似乎已经解决了!我按照您的建议在else块中创建了一个新的出价,并使用更新后的views.py编辑了我的问题,您可以看到。我相信很快又会遇到另一个问题,但是感谢您帮助我解决这个问题 :) - sarchi-xo
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接