如何从新视图访问其他模型字段

时间:2021-03-21 21:50:25

标签: python django django-models django-views django-templates

我正在尝试建立一个愿望清单,我有一个列表模型和一个愿望清单模型, 我已经成功地让它工作了,但是..

我必须首先遍历所有列表并与我的愿望清单中的产品 ID 匹配,以便我可以访问标题和图像等字段..

有没有比遍历所有列表直到找到匹配的列表更简单的方法?

views.py

def add_to_wishlist(request, product_id):
    product = WishList.objects.filter(listing_id=product_id, user=request.user.username)
    if product:
        product.delete()
    else:
        product = WishList()
        product.listing_id = product_id
        product.user = request.user.username
        product.save()
    return HttpResponseRedirect(request.META['HTTP_REFERER'])


def wishlist(request):
    product = WishList.objects.filter(user=request.user)
    all_listings = AuctionListing.objects.all()
    return render(request, "auctions/wishlist.html", {
        'wishlist': product,
        'all_listings': all_listings
    })

models.py

class AuctionListing(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    start_bid = models.IntegerField(null=True)
    image = models.CharField(max_length=1000, blank=True)
    category = models.CharField(max_length=100)
    seller = models.CharField(max_length=100, default="Default_Value")


class WishList(models.Model):
    user = models.CharField(max_length=64)
    listing_id = models.IntegerField()

wishlist.html

{% extends "auctions/layout.html" %}
{% block title %}Users Wishlist {% endblock %}
{% block body %}
<div class="col-12 mx-auto">
    <h1 class="h3">My Wishlist</h1>
    <div>Manage your wishlist</div>
    {% if wishlist %}
         {% for listing in all_listings %}
            {% for product in wishlist %}
                {% if listing.id == product.listing_id%}
                    <div class="card-mb-3">
                        <div class="row g-0">
                            <div class="col-md-4">
                                <img src="{{ listing.image }}" class="img-responsive" >
                            </div>
                            <div class="col-md-8">
                                <div class="card-body">
                                    <h5 class="card-title">{{listing.title}}</h5>
                                    <p class="card-text"> {{listing.description}}</p>
                                    <p class="card-text"><small class="text-muted">{{listing.start_bid}}</small></p>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endif %}
            {% endfor %}
        {% endfor %}

    {% else %}
        <p class="card-text">No products have been added to your wishlist yet</p>
    {% endif %}
</div>
{% endblock %}

1 个答案:

答案 0 :(得分:2)

因为您正在为用户获取所有 WishList 对象,所以您不需要只收集所有 AuctionListing 对象并对其进行迭代。您可以使用返回的 WishLists 上的 ID 查询它们。

我会做类似的事情;

views.py

def wishlist(request):
    wishlists = WishList.objects.filter(user=request.user)
    listing_ids = wishlists.values_list('listing_id', flat=True)
    listings = AuctionListing.objects.filter(pk__in=listing_ids)
    
    return render(request, "auctions/wishlist.html", {
        'wishlists': wishlists,
        'listings': listings
    })

wishlist.html

{% extends "auctions/layout.html" %}
{% block title %}Users Wishlist {% endblock %}
{% block body %}
<div class="col-12 mx-auto">
    <h1 class="h3">My Wishlist</h1>
    <div>Manage your wishlist</div>
    {% if wishlists and listings %}

          {% for listing in listings %}
                    <div class="card-mb-3">
                        <div class="row g-0">
                            <div class="col-md-4">
                                <img src="{{ listing.image }}" class="img-responsive" >
                            </div>
                            <div class="col-md-8">
                                <div class="card-body">
                                    <h5 class="card-title">{{listing.title}}</h5>
                                    <p class="card-text"> {{listing.description}}</p>
                                    <p class="card-text"><small class="text-muted">{{listing.start_bid}}</small></p>
                                </div>
                            </div>
                        </div>
                    </div>
         {% endfor %}
    {% else %}
        <p class="card-text">No products have been added to your wishlist yet</p>
    {% endif %}
</div>
{% endblock %}