我不确定如何解决这个问题。我尝试将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 })
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-xoBid
和Listing
有什么关系?你能分享相关的模型吗? - Willem Van Onsem['bid_input__max']
吗? - Willem Van OnsemNone
类型表示在那个时刻没有出价。您可以使用... or ...
表达式将其替换为零。请参见编辑。 - Willem Van Onsem