Alwayslearning
Alwayslearning

Reputation: 113

How to access other model fields from new View

I'm trying to build a Wish list, I have a model for the listings, and a model for the wish list, I have succeeded in making it work but..

I have to loop over all the listings first and match with the product id in my wish list so i can access the fields such as title and image..

is there any easier way to do this than looping over all the listings until finding the matched ones ?

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 %}

Upvotes: 0

Views: 39

Answers (1)

markwalker_
markwalker_

Reputation: 12849

Because you're getting all the WishList objects for the user, you don't need to just gather all AuctionListing objects and iterate over them. You can query them using the IDs on the WishLists you get back.

I'd do something like;

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 %}

Upvotes: 2

Related Questions