模型关系Django

发布于 2025-02-12 15:36:27 字数 2699 浏览 2 评论 0 原文

感谢您的帮助。我有两个型号。在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 %}

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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2025-02-19 15:36:27

在您的模板中尝试一下:

{{ post.l_auctions.get.current_bid_cmp }}

更新

我对您的模型进行了一些修改,以便它们对我更有意义。您可能需要基本模型是有充分理由的,因此请随时修改您的需求。我也更改了一些名称。我看到的事情仍然没有很多意义,但我会把它留给你。

model.py.py

from django.db import models
from django.conf import settings

class BaseModel(models.Model):
    pass

class Category(models.Model):
    name = models.CharField(max_length=20)

class Listing(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    title = models.CharField('Title of the auction', max_length=150, unique=True)
    description = models.TextField('Description')
    starting_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')

    def __str__(self):
        return self.title

class Bid(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='offer_user')
    listing = models.ForeignKey(Listing, null=True, on_delete=models.CASCADE, related_name='bids')
    initialized_bid = models.BooleanField(default=False)
    amount = models.FloatField('Current Bid', blank=True, null=True)
    offer = models.FloatField(default=0, null=True, blank=True)

    def __str__(self):
        return str(self.amount)

    class Meta:
        get_latest_by = 'amount'

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% for listing in object_list %}
    <div class=" col-md-4 mt-4 ">

        <div class="card " style="width: 18rem;">
            <a href="#">
                <img class="card-img-top img-fluid" src="{{ listing.image.url }}" alt="Card image cap">
            </a>

            <div class="card-body">
                <a class="darklink" href="#">
                    <h5 class="card-title">{{ listing.title }}</h5></a>
                <h5>Starting bid: $ {{ listing.starting_bid }}</h5>
                <h5>Current bid: $ {{ listing.bids.latest }}</h5>
                <p class="card-text">{{ listing.content | truncatechars:700 }}</p>
                <a href="#" class="btn btn-danger btn-block">Show Auction</a>
            </div>
        </div>

    </div>
{% endfor %}

</body>
</html>

这是来自Shell的交互式会话:

from django.contrib.auth import get_user_model
User = get_user_model()
admin = User.objects.get_or_create(username='admin')[0]
steve = User.objects.get_or_create(username='steve')[0]
jenny = User.objects.get_or_create(username='jenny')[0]
almond = User.objects.get_or_create(username='almond')[0]
from auctions.models import Category, Listing, Bid
c = Category.objects.get_or_create(name='General')[0]

l1 = Listing.objects.get_or_create(user=admin, category=c, title='Some title', description='description', starting_bid=1.00, content='content')[0]

b1 = Bid.objects.get_or_create(user=steve, listing=l1, amount=1.00)[0]
b2 = Bid.objects.get_or_create(user=jenny, listing=l1, amount=1.01)[0]
b3 = Bid.objects.get_or_create(user=almond, listing=l1, amount=1.02)[0]

>>> l1.bids.all()
<QuerySet [<Bid: 1.0>, <Bid: 1.01>, <Bid: 1.02>]>

您可以通过:

将其添加到您的类中:

    class Meta:
        get_latest_by = 'amount'

以及使用 listing.bids。最新() ...

或使用汇总:

from django.db.models import Max
>>> l1.bids.aggregate(max=Max('amount'))
{'max': 1.02}

要注意的关键是, listing.bids 返回a “相关manager” 。这只是意味着您可以使用熟悉的QuerySet方法,例如 .all() .get() .filter()。 last() .latest()等。

在您的情况下,您应该首先在。然后决定您要如何进行。在上面的示例中,我将类Meta 放在 bid 模型上,该模型使您可以根据金额恢复最新对象。假设最新金额始终是最高的,这可能对您的情况不正确。

您可以做的另一件事是将 @property 添加到您的 listing 模型。

class Listing(models.Model):

    ...

    @property
    def max_bid(self):
        from django.db.models import Max
        max_bid = self.bids.aggregate(max=Max('amount'))
        if max_bid.get('max'):
            return max_bid['max']
        return ""

在您的模板中使用:

<h5>Current bid: $ {{ listing.max_bid }}</h5>

Try this in your template:

{{ post.l_auctions.get.current_bid_cmp }}

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

from django.db import models
from django.conf import settings

class BaseModel(models.Model):
    pass

class Category(models.Model):
    name = models.CharField(max_length=20)

class Listing(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    title = models.CharField('Title of the auction', max_length=150, unique=True)
    description = models.TextField('Description')
    starting_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')

    def __str__(self):
        return self.title

class Bid(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='offer_user')
    listing = models.ForeignKey(Listing, null=True, on_delete=models.CASCADE, related_name='bids')
    initialized_bid = models.BooleanField(default=False)
    amount = models.FloatField('Current Bid', blank=True, null=True)
    offer = models.FloatField(default=0, null=True, blank=True)

    def __str__(self):
        return str(self.amount)

    class Meta:
        get_latest_by = 'amount'

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% for listing in object_list %}
    <div class=" col-md-4 mt-4 ">

        <div class="card " style="width: 18rem;">
            <a href="#">
                <img class="card-img-top img-fluid" src="{{ listing.image.url }}" alt="Card image cap">
            </a>

            <div class="card-body">
                <a class="darklink" href="#">
                    <h5 class="card-title">{{ listing.title }}</h5></a>
                <h5>Starting bid: $ {{ listing.starting_bid }}</h5>
                <h5>Current bid: $ {{ listing.bids.latest }}</h5>
                <p class="card-text">{{ listing.content | truncatechars:700 }}</p>
                <a href="#" class="btn btn-danger btn-block">Show Auction</a>
            </div>
        </div>

    </div>
{% endfor %}

</body>
</html>

Here's an interactive session from a shell:

from django.contrib.auth import get_user_model
User = get_user_model()
admin = User.objects.get_or_create(username='admin')[0]
steve = User.objects.get_or_create(username='steve')[0]
jenny = User.objects.get_or_create(username='jenny')[0]
almond = User.objects.get_or_create(username='almond')[0]
from auctions.models import Category, Listing, Bid
c = Category.objects.get_or_create(name='General')[0]

l1 = Listing.objects.get_or_create(user=admin, category=c, title='Some title', description='description', starting_bid=1.00, content='content')[0]

b1 = Bid.objects.get_or_create(user=steve, listing=l1, amount=1.00)[0]
b2 = Bid.objects.get_or_create(user=jenny, listing=l1, amount=1.01)[0]
b3 = Bid.objects.get_or_create(user=almond, listing=l1, amount=1.02)[0]

>>> l1.bids.all()
<QuerySet [<Bid: 1.0>, <Bid: 1.01>, <Bid: 1.02>]>

You could get the max by:

Adding this to your class:

    class Meta:
        get_latest_by = 'amount'

and using listing.bids.latest()...

Or using aggregate:

from django.db.models import Max
>>> l1.bids.aggregate(max=Max('amount'))
{'max': 1.02}

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 the Bid 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 your Listing model.

class Listing(models.Model):

    ...

    @property
    def max_bid(self):
        from django.db.models import Max
        max_bid = self.bids.aggregate(max=Max('amount'))
        if max_bid.get('max'):
            return max_bid['max']
        return ""

Use in your template like this:

<h5>Current bid: $ {{ listing.max_bid }}</h5>
岁月染过的梦 2025-02-19 15:36:27

在您的Jinja模板中使用

 {{object_list.0.l_auctions.current_bid_cmp}}

Use in your Jinja Template

 {{object_list.0.l_auctions.current_bid_cmp}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文