Home > Mobile >  printing values django templates using for loop
printing values django templates using for loop

Time:12-05

I have two models interrelated items and broken :

class Items(models.Model):
    id = models.AutoField(primary_key=True)
    item_name = models.CharField(max_length=50, blank=False)
    item_price = models.IntegerField(blank=True)
    item_quantity_received = models.IntegerField(blank=False)
    item_quantity_available = models.IntegerField(blank=True)
    item_purchased_date = models.DateField(auto_now_add=True, blank=False)
    item_units = models.CharField(max_length=50, blank=False)

    def __str__(self):
        return self.item_name

class Broken(models.Model):
    item = models.ForeignKey(Items, default=1,  on_delete=models.CASCADE)
    item_quantity_broken = models.IntegerField(blank=True)
    item_broken_date = models.DateField(auto_now_add=True, blank=False)
    item_is_broken = models.BooleanField(default=True)
    date_repaired = models.DateField(auto_now=True, blank=True)

    def __str__(self):
        return self.item.item_name

I wrote this view function to retrieve data to a table into a template:


    def broken_items(request):
    br = Broken.objects.select_related('item').all()
    print(br.values_list())
    context = {
        'title': 'broken',
        'items': br,
    }

    return render(request, 'store/broken.html', context)

this is the executing query:

    SELECT "store_broken"."id",
       "store_broken"."item_id",
       "store_broken"."item_quantity_broken",
       "store_broken"."item_broken_date",
       "store_broken"."item_is_broken",
       "store_broken"."date_repaired",
       "store_items"."id",
       "store_items"."item_name",
       "store_items"."item_price",
       "store_items"."item_quantity_received",
       "store_items"."item_quantity_available",
       "store_items"."item_purchased_date",
       "store_items"."item_units"
       FROM "store_broken"
       INNER JOIN "store_items"
        ON ("store_broken"."item_id" = "store_items"."id")

looks like it gives me all the fields I want. In debugger it shows data from both tables, so I wrote for loop in template,

    {% for item in items %}
            <tr>
                <td>{{item.id}}</td>
                <td>{{item.item_id}}</td>
                <td>{{item.item_quantity_broken}}</td>
                <td>{{item.item_broken_date}}</td>
                <td>{{item.item_is_broken}}</td>
                <td>{{item.date_repaired}}</td>
                <td>{{item.item_name }}</td>
                <td>{{item.item_item_quantity_received}}</td>
                <td>{{item.item_quantity_available}}</td>
                <td>{{item.item_purchased_date}}</td>
                <td>{{item.items_item_units}}</td>
            </tr>
     {% endfor %}

The thing is this loop only gives me data from broken table only. I can't see data from Items table.

enter image description here

enter image description here

can someone help me to find the reason why other details are not showing?

CodePudding user response:

Your items query is of Broken objects. So in order to access the Items values you need to change your table. For better understanding change your view like this:

brokens = Broken.objects.select_related('item').all()
context = {
    'title': 'broken',
    'brokens ': brokens,
}

and then your table:

{% for broken in brokens %}
        <tr>
            <td>{{broken.id}}</td>
            <td>{{broken.item.pk}}</td> # This is the item id 
            <td>{{broken.item_quantity_broken}}</td>
            <td>{{broken.item_broken_date}}</td>
            <td>{{broken.item_is_broken}}</td>
            <td>{{broken.date_repaired}}</td>
            <td>{{broken.item.item_name}}</td>
            <td>{{broken.item.item_quantity_received }}</td>
            <td>{{broken.item.item_quantity_available}}</td>
            <td>{{broken.item.item_purchased_date}}</td>
            <td>{{broken.item.items_item_units}}</td>
        </tr>
 {% endfor %}

CodePudding user response:

you loop over a List of Broken objects to access the related item objects

item.item.item_name

  • Related