I am Facing Problem in Query data from mysql DB

models.py code

class AddProductServices(models.Model):

    userId = models.CharField(max_length=200)

    productName = models.CharField(max_length=200)

    sectorId = models.CharField(max_length=200)

    categoryId = models.CharField(max_length=200)

    subCatId = models.CharField(max_length=200)

    imageUrl_1 = models.ImageField(upload_to='productimages/')

    imageUrl_2 = models.ImageField(upload_to='productimages/')

    productPrice = models.CharField(max_length=200)

    productHighlights = models.CharField(max_length=5000)

    productDescription = models.CharField(max_length=5000)

    date = models.DateTimeField()

class AffliateLink(models.Model):

    userId = models.CharField(max_length=200)

    productId = models.CharField(max_length=200)

    affliateCode = models.CharField(max_length=200)
  1. I am using mysql database

my logged user id is 7 so 3 items shows in views.py terminal its showing 3 products
when i add code in dataArray452 ‘d’: d then loop all the products in django html its showing only 1 item
how to show all items as printing in terminal
pls help me here how to solve this problem

Please post your view and template that you are using. (Please post the text, not images.)


def interestedAffliateList(request):
    listAffliate = AffliateLink.objects.filter(userId = request.session.get('userLoggedUserId')).all()

    for i in listAffliate:
        affliateProductFetch = AddProductServices.objects.filter(id = i.productId).all()
        for dfetch in affliateProductFetch:
            print(f"i -> ", i.productId , ", d -> ",dfetch.productName)
    dataArray452 = {
        'dfetchh': dfetch
    return render(request, "userTemplates/interestedAffliateList.html", dataArray452)


{% for ab in dfetchh %}
    <div class="col-4">
        <h4> {{ab.productName}} </h4>
{% endfor %}

i am getting error when i reload
error is TypeError at /interestedAffliateList ‘AddProductServices’ object is not iterable

and when i remove these codes

{% for ab in dfetchh %}
    <div class="col-4">
        <h4> {{ab.productName}} </h4>
{% endfor %}

and add this h4 tag

	<h4> {{dfetchh.productName}} </h4>

then error goes i am getting this output in my terminal

[22/Oct/2021 19:08:09] "GET /interestedAffliateList HTTP/1.1" 200 4002
i ->  27 , d ->  samsung Fridge 27
i ->  32 , d ->  SAMSUNG Galaxy Z Fold3 5G 32
i ->  31 , d ->  LG Washing Machine 31

and in page  its showing only 1 product 

LG Washing Machine 31

only but i want to print all 3 items on the page How to to this ???

please check my models.py

i want to print affliateCode with product name and id as printing in terminal
how to do this

I think the first thing that you should do is to modify your schema such that relationships between tables are properly maintained. It appears as if there’s an implied ForeignKey between AddProductServices to AffliateLink, but you don’t have an actual ForeignKey defined. (More accurately, it looks like both are related to your User object, in which case both should have an FK to User.)

Ensuring your relationships are defined appropriate is going to make everything else a whole lot easier. At that point, you can use the relationship managers to get related objects.

thanks sir for reply