检查哪一个小数点较大django



我的一行代码有问题。。。我试图检查哪一个小数更大,我检查了stackoverflow的一些答案,但它们不起作用(可能是因为它们是7年前的:P(无论如何,这是我的观点:

auction = all_auctions.objects.get(id= my_id)
comment_num = auction.comments.all().count()
all_comments = auction.comments.all()
context = {
"auction": auction,
"bid_auction": auction.bid.all(),
"comment_num": comment_num,
"all_comments": all_comments
}
if request.method == "POST" and "bid" in request.POST:
if request.user.is_authenticated:
highest_bid = request.POST["bid"]
if not Decimal(highest_bid) <= Decimal(int(auction.bid.all().reverse()[0])):
all_bid = bids.objects.create(bid = highest_bid)
auction.bid.add(all_bid)
if request.method == "POST" and "watch"in request.POST:
if request.user.is_authenticated:
if auction not in request.user.watchlist.all():
request.user.watchlist.add(auction)
else:
request.user.watchlist.remove(auction)
if request.method == "POST" and "comment" in request.POST:
comment = request.POST["comment"]
if request.user.is_authenticated:
comment_now = comments.objects.create(comment = comment)
auction.comments.add(comment_now)
return render(request, "auctions/dynamic_auction.html", context)

还有一条有问题的线路:

if not Decimal(highest_bid) <= Decimal(int(auction.bid.all().reverse()[0])):

在模型中:

from django.contrib.auth.models import AbstractUser
from django.db import models
class comments(models.Model):
comment = models.TextField(blank=False)
class bids(models.Model):
bid = models.DecimalField(decimal_places = 2, max_digits = 100000)
class all_auctions(models.Model):
category = models.CharField(max_length= 14, default = "none")
title = models.CharField(max_length= 14)
description = models.TextField(blank=False)
photo_url = models.CharField(max_length= 500000)
bid = models.ManyToManyField(bids, blank = True, related_name = "bid_auction")
comments = models.ManyToManyField(comments, blank = True, related_name = "comments")
class User(AbstractUser):
created =  models.ManyToManyField(all_auctions, blank = True, related_name = "created")
watchlist = models.ManyToManyField(all_auctions, blank = True, related_name = "watchlist")
user_bid = models.ManyToManyField(bids, blank = True, related_name = "user_bid")

在模板中,我可以出价并查看它们:

{% extends "auctions/layout.html" %}
{% block body %}
<div>
<h2>{{auction.title}}</h2>
<div style = "width: 1400px; padding: 10px; margin: 10px;" class = auction_class>
<img src="{{auction.photo_url}}" alt="no image">
<div>
{{auction.description}}
<p></p>
{% for bid in bid_auction %}
{% if forloop.last %}
bid:{{bid.bid}}
{% endif %}
{% endfor %}
<p></p>
{{auction.category}}
<p></p>
{% if user.is_authenticated %}
<form method = "POST"> {% csrf_token %}
<input class="btn btn-primary" type="submit" name = "watch" value="Add to watchlist">
</form>
<form  method = "POST"> {% csrf_token %}
<div>
<input style = "margin:10px;" class="form-control" name = "bid" type="text" placeholder= "Bid...">
<input class="btn btn-primary" type="submit" value="Place Bid">
</div>
</form>
<form  method = "POST"> {% csrf_token %}
<div>
<input style = "margin:10px;" class="form-control" name = "comment" type="text" placeholder= "Comment...">
</div>
</form>
{% endif %}
<p></p>
comments:
<p></p>
{% if comment_num == 0 %}
no comments
{% endif %}
<p></p>
<p></p>
{% for com in all_comments %}
{{ com.comment }}
<p></p>
{% endfor %}
</div>
</div>
</div>
{% endblock %}

是的,我做到了,所以显示了bids查询集的最后一个对象,我不知道如何显示最大的对象,所以我也很感激帮助。谢谢:D

我猜这是:

if not Decimal(highest_bid) <= Decimal(int(auction.bid.all().reverse()[0].bid)): 

应该有效(最后请注意.bid(。

为什么不能使用[-1]

虽然我还没有使用过django,但由于您想比较小数,从技术上讲,您可以创建一个继承类Decimal()的类,然后返回一个浮点值并比较该浮点值。虽然我认为这行不通(我没有尝试过,因为我不特别喜欢django(,但这可能是一种可能性。

编辑:使用函数从类中返回浮点值。

示例代码:

class DecimalNumber(Decimal):
def __init__(self, value, par2...):
self.value = value
...
def return_float(self):
float = self.value
return float

最新更新