Home > front end >  Django: How do I access a related object max value in html template
Django: How do I access a related object max value in html template

Time:12-22

I am attempting to display a list of items for auction. For each item, I am wanting to also display the current bid price. The current bid price should be the max value or last added to the Bids class for each individual listing.

How do I render in my HTML the max Bid.bid_price for each item in my Listing.objects.all() collection?

Below are my models, views, and HTML.

Models:

class Listing(models.Model):
    title = models.CharField(max_length=65, default="")
    description = models.CharField(max_length=200, default="")
    category = models.CharField(max_length=65, default="")
    image = models.ImageField(blank=True, null=True, upload_to='images/')
    listed_by = models.ForeignKey(User, on_delete=models.CASCADE, default="")
    created_dt_tm = models.DateTimeField(auto_now_add=False, default=timezone.now())

class Bids(models.Model):
    bidder = models.ForeignKey(User, on_delete=models.CASCADE, default="")
    listing = models.ForeignKey(Listing, on_delete=models.CASCADE, default="", related_name="bids")
    bid_price = models.IntegerField(default=0)
    created_dt_tm = models.DateTimeField(auto_now_add=False, default=timezone.now())

Views:

def index(request):
    return render(request, "auctions/index.html", {
        "listings": Listing.objects.all()
    })

HTML:

    {% for listing in listings %}
    <div style=display:flex;margin:30px;border:lightseagreen;border-style:solid;height:150px;border-width:1px;width:40%>
        
        <div style=width:25%;display:flex;>
            {% if listing.image %}
            <img src="{{listing.image.url}}" alt="Cannot display image" height="100px" style="margin-left:50px;">
            {% endif %}
        </div>
        <div style=width:15%></div>
        <div style=width:30%>
            <div><a href="{% url 'listing' listing.title %}">{{listing.title}}</a></div>
            <div style=font-size:10pt;>Description:  {{listing.description}}</div>
            <div style=font-size:10pt;>Category:  {{listing.category}}</div>
            <div style=font-size:10pt;>Current Price: ${{ listing.bids_object.bid_price }}</div>
            <div style=font-size:10pt;>Created by:<br>{{listing.listed_by}}</div>
        </div>
        <div style=width:30%>
            <div style=margin:10px;>
            </div>
        </div>       
    </div>
    {% endfor %}
{% endblock %}

CodePudding user response:

what you need is django's annotate functionality to find the max of each Listing.

from django.db.models import Max

def index(request):
    return render(request, "auctions/index.html", {
        "listings":  Listing.objects.all().values().annotate(
                max_bid_amount = Max('bids__bid_price')
                )
    })

in your html you can then add following line for max_bid.

<div style=font-size:10pt;>Max Bid Amount:<br>{{listing.max_bid_amount}}</div>

  • Related