I wanna the last input to bid_price
to be received in the Bid_info
model and attributed to bid_max
, however it gives this error
earliest() and latest() require either fields as positional arguments or 'get_latest_by' in the model's Meta.
views:
@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>
models:
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)
The first problem is what you use to order. You can use the primary key, but that is often a tricky choice: the primary key is not per se assigned in a linear manner.
If you want to get the bid with the largest bid_price
, you use:
@login_required
def page_product(request, productid):
bid_max = Bid_info.objects.filter(product_id=productid).latest('bid_price')
context = {'bid_max': bid_max}
return render(request, 'auctions/page_product.html', context)
This will thus return the Bid_info
object for the product with the highest bid_price
.
Note: Models in Django are written in PascalCase, not snake_case, so you might want to rename the model from
toBid_info
BidInfo
.
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.