I'd appreciate your help. I have two models. In the Bids model you will find current_bid_cmp. I would like from the ListingAuctions model to be able to access the corresponding current_bid_cmp of the Bids model. I only have found information to do querys from the model which contains the foreingKey.
My view: I use get_context_data because I have been trying anothers querys. perhaps its not the more aproppiate
class index(ListView):
model = ListingAuctions
template_name = "auctions/index.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['object'] = ListingAuctions.objects.all()
return context
My Models:
class ListingAuctions(BaseModel):
title_auction = models.CharField('Title of the auction',max_length = 150, unique = True)
description = models.TextField('Description')
user = models.ForeignKey(User, on_delete = models.CASCADE)
category = models.ForeignKey(Category, on_delete = models.CASCADE)
initial_bid = models.FloatField(default=False)
content = models.TextField('Content of auction')
image = models.ImageField('Referential Image', null=True,
upload_to = 'images_auctions', max_length = 255, default= 'noimage.png')
class Bids(BaseModel):
user = models.ForeignKey(User, on_delete = models.CASCADE, related_name='offer_user')
listing_auctions = models.ForeignKey(ListingAuctions,null=True, on_delete= models.CASCADE, related_name='l_auctions')
initialized_bid = models.BooleanField(default=False)
current_bid_cmp = models.FloatField('Current Bid Cmp',blank=True, null=True )
offer = models.FloatField(default=0 ,null=True, blank=True)
My HTML
Current bid: $ {{post.l_auctions.current_bid_cmp}}
its my attemp l_auctions is the relate name from listing_auctions in Bids model. Post is a ListingAuction object:{% for post in object_list %}
<div >
<div style="width: 18rem;">
<a href="{% url 'auctions_detail' post.pk %}">
<img src="{{ post.image.url }}" alt="Card image cap" >
</a>
<div >
<a href="{% url 'auctions_detail' post.pk %}"> <h5 >{{post.title_auction}}</h5></a>
<h5>Starting bid: $ {{post.initial_bid}}</h5>
<h5>Current bid: $ {{post.l_auctions.current_bid_cmp}}</h5>
<p >{{post.content | truncatechars:700 }}</p>
<a href=" {% url 'auctions_detail' post.pk %}" >Show Auction</a>
</div>
</div>
</div>
{% endfor %}
CodePudding user response:
Try this in your template:
{{ post.l_auctions.get.current_bid_cmp }}
CodePudding user response:
Use in your Jinja Template
{{object_list[0].l_auctions.current_bid_cmp}}