从模型获取最新输入

问题描述 投票:0回答:1

我希望

bid_price
的最后一个输入在
Bid_info
模型中接收并归因于
bid_max
,但是它给出了此错误

earliest() 和latest() 需要字段作为位置参数或模型元数据中的“get_latest_by”。

浏览量:

@login_required
def page_product(request, productid):
    bid_max = Bid_info.objects.filter(pk=productid).latest()
    context={
        'bid_max' : bid_max      
    }
return render(request, 'auctions/page_product.html', context)

html:

<p> final price = {{bid_max}} </p>

型号:

class Bid_info(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name="seller")
    bid_price = models.DecimalField(max_digits=10, decimal_places=2)
    checkclose = models.BooleanField(default=False)
    winner = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, 
                                                                blank=True)
django django-models django-views django-templates
1个回答
0
投票

通过使用

bid_max = Bid_info.objects.filter(pk=productid).latest()

只给你1个对象,因为你使用“pk”来过滤。对于每个对象来说都是独一无二的。所以你无法访问最新、最后、第一。

使用

p = Article.objects.filter(some_field=value).order_by("bid_price").first() 


Or

p = Article.objects.all().order_by("bid_price").first() 

获得第一个物体。

使用

p = Article.objects.filter(some_field=value).order_by("bid_price").last() 


Or

Or

p = Article.objects.all().order_by("bid_price").last()
 

获取最后一个对象。

© www.soinside.com 2019 - 2024. All rights reserved.