Python Django中模型中的访问值
一周前我开始学习django,我试图从django中的一个模型获取bid_值,但它返回以下错误: /item/2“QuerySet”对象的AttributeError没有属性 “投标价值” 我试过几种方法,但都不管用 models.py:Python Django中模型中的访问值,python,django,Python,Django,一周前我开始学习django,我试图从django中的一个模型获取bid_值,但它返回以下错误: /item/2“QuerySet”对象的AttributeError没有属性 “投标价值” 我试过几种方法,但都不管用 models.py: class Auction_listings(models.Model): product_image = models.CharField(max_length=500) product_title = models.CharField(ma
class Auction_listings(models.Model):
product_image = models.CharField(max_length=500)
product_title = models.CharField(max_length=40)
product_description = models.CharField(max_length=200)
product_category = models.CharField(max_length=20, default="others")
product_price = models.FloatField()
is_closed = models.BooleanField(default=False)
username = models.CharField(max_length=64)
post_date = models.DateField(auto_now_add=True)
def __str__(self):
return f"{self.product_title}"
class Bids(models.Model):
auction = models.ForeignKey(Auction_listings, on_delete=models.CASCADE)
username = models.CharField(max_length=64)
bid_value = models.FloatField()
views.py:
def add_bid(request, item_id):
username = None
if request.user.is_authenticated:
username = request.user.username
if request.method == "POST":
bid = Bids.objects.filter(auction=item_id).bid_value
new_bid_value = float(request.POST.get("bid"))
if new_bid_value > float(bid):
new_bid = Bids(auction=item_id, username=username, bid_value=new_bid_value)
new_bid.save()
else:
return render(request, "auctions/error.html", {
"error": "your bid is lower than the current bid..."
})
def view_item(request, id):
item_id = Auction_listings.objects.get(pk=id)
add_bid(request, item_id)
try:
bid = Bids.objects.get(auction=item_id)
return render(request, "auctions/item.html", {
"auctions": item_id,
"bid": bid
})
except:
return render(request, "auctions/item.html", {
"auctions": item_id,
})
感谢您在advanced中尝试访问
QuerySet
的bid\u value
属性,而不是模型
您正试图从列表中获取值。由于Bids
模型的auction
属性是ForeignKeyField
,因此查询返回一个列表,而不是一个实例。您需要从Bids.objects
if request.method == "POST":
bid = Bids.objects.filter(auction=item_id).aggregate(Max('bid_value'))['bid_value__max']
.....
您试图访问的是
QuerySet
的bid\u value
属性,而不是模型
您正试图从列表中获取值。由于Bids
模型的auction
属性是ForeignKeyField
,因此查询返回一个列表,而不是一个实例。您需要从Bids.objects
if request.method == "POST":
bid = Bids.objects.filter(auction=item_id).aggregate(Max('bid_value'))['bid_value__max']
.....
关于代码失败原因的线索当然在错误中 “位于/item/2'QuerySet'对象的AttributeError没有'bid_value'属性” 你的错误在这里:
bid = Bids.objects.filter(auction=item_id).bid_value
使用Bids.objects.filter(auction=item\u id)创建Queryset对象。
我喜欢:
QuerySet本质上是给定模型的对象列表。QuerySet允许您从数据库中读取数据,对其进行筛选和排序
您需要访问模型上的bid_value属性。因此,一种方法是获取从queryset返回的第一个值:
bid = Bids.objects.filter(auction=item_id).first().bid_value
更好的方法是检查对象是否存在,然后访问bid_value属性:
bid = Bids.objects.filter(auction=item_id).first()
if bid:
bid = bid.bid_value
快乐学习!:) 关于代码失败原因的线索当然在错误中 “位于/item/2'QuerySet'对象的AttributeError没有'bid_value'属性” 你的错误在这里:
bid = Bids.objects.filter(auction=item_id).bid_value
使用Bids.objects.filter(auction=item\u id)创建Queryset对象。
我喜欢:
QuerySet本质上是给定模型的对象列表。QuerySet允许您从数据库中读取数据,对其进行筛选和排序
您需要访问模型上的bid_value属性。因此,一种方法是获取从queryset返回的第一个值:
bid = Bids.objects.filter(auction=item_id).first().bid_value
更好的方法是检查对象是否存在,然后访问bid_value属性:
bid = Bids.objects.filter(auction=item_id).first()
if bid:
bid = bid.bid_value
快乐学习!:) Bids.objects.filter(拍卖=item\u id)。bid\u值没有意义,因为这是Bids
对象的集合,而不是单个bid
对象。Bids.objects.filter(拍卖=item\u id)。bid\u值没有意义,因为这是Bids
对象的集合,没有一个Bid
对象。