模型关系Django
感谢您的帮助。我有两个型号。在BIDS模型中,您会找到Current_BID_CMP。我希望从Listingauctions模型中访问BIDS模型的相应Current_BID_CMP。我只找到了来自包含oferingkey的模型的信息。
我的观点:我使用get_context_data,因为我一直在尝试Anothers查询。也许不是
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
我的模型越多:
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)
我的HTML
电流出价:$ {{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 class=" col-md-4 mt-4 ">
<div class="card " style="width: 18rem;">
<a href="{% url 'auctions_detail' post.pk %}">
<img class="card-img-top img-fluid" src="{{ post.image.url }}" alt="Card image cap" >
</a>
<div class="card-body">
<a class="darklink" href="{% url 'auctions_detail' post.pk %}"> <h5 class="card-title">{{post.title_auction}}</h5></a>
<h5>Starting bid: $ {{post.initial_bid}}</h5>
<h5>Current bid: $ {{post.l_auctions.current_bid_cmp}}</h5>
<p class="card-text">{{post.content | truncatechars:700 }}</p>
<a href=" {% url 'auctions_detail' post.pk %}" class="btn btn-danger btn-block">Show Auction</a>
</div>
</div>
</div>
{% endfor %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的模板中尝试一下:
更新
我对您的模型进行了一些修改,以便它们对我更有意义。您可能需要基本模型是有充分理由的,因此请随时修改您的需求。我也更改了一些名称。我看到的事情仍然没有很多意义,但我会把它留给你。
model.py.py
index.html
这是来自Shell的交互式会话:
您可以通过:
将其添加到您的类中:
以及使用
listing.bids。最新()
...或使用汇总:
要注意的关键是,
listing.bids
返回a “相关manager” 。这只是意味着您可以使用熟悉的QuerySet方法,例如.all()
,.get()
,.filter()
,。 last()
,.latest()
等。在您的情况下,您应该首先在。然后决定您要如何进行。在上面的示例中,我将
类Meta
放在bid
模型上,该模型使您可以根据金额恢复最新对象。假设最新金额始终是最高的,这可能对您的情况不正确。您可以做的另一件事是将
@property
添加到您的listing
模型。在您的模板中使用:
Try this in your template:
Update
I modified your models a bit so they make more sense to me. You might need your BaseModel for a good reason so feel free to modify to your needs. I also changed some names. There's still things I see that don't make a lot of sense in them but I'll leave that to you.
models.py
index.html
Here's an interactive session from a shell:
You could get the max by:
Adding this to your class:
and using
listing.bids.latest()
...Or using aggregate:
The key thing to note is,
listing.bids
returns a "RelatedManager". This just means you can use familiar queryset methods like.all()
,.get()
,.filter()
,.last()
,.latest()
, etc. Many more.In your case, you should first review this article on how to get the max of a queryset. Then decide how you want to proceed. In the example above, I put a
class Meta
on theBid
model which lets you get the latest object back based on the amount. This assumes the latest amount is always the highest, which might not be true for your situation.One other thing you could do is add a
@property
to yourListing
model.Use in your template like this:
在您的Jinja模板中使用
Use in your Jinja Template