1

I have 2 tables:

Table1: [A-Foriegn Key, field1]
Table2: [Table1-Foriegn Key, field2]

I want to display a list with distinct values of Table1 field1 if items are present in Table2. and inside that list I want to display all the elements of Table2 corresponding to that field1 of Table1.

I am trying :

{% for t1 in Table1 %}
   Display field1 of t1
   {% for t2 in Table2 %}
      Display field2 of t2.
   {% endfor %}
{% endfor %}

Table 1:

╔════╦══════════════╦══════╗
║ Id ║  EventName   ║ FK   ║
╠════╬══════════════╬══════╣
║  1 ║ Christmas    ║  56  ║
║  2 ║ Black Friday ║  18  ║
╚════╩══════════════╩══════╝

Table 2:

╔════╦══════════════╦══════╗
║ ID ║  Image       ║ FK   ║
╠════╬══════════════╬══════╣
║  1 ║ image1_url   ║  1   ║
║  2 ║ image2_url   ║  1   ║
║  3 ║ image3_url   ║  2   ║
║  4 ║ image4_url   ║  2   ║
╚════╩══════════════╩══════╝

I want to display : Christmas and BlackFriday in a list.

And on clicking element from the list, I want to display all url for that particular event. Like on clicking Christmas: image1_url and image2_url should be displayed.

I dont want to make AJAX requests.

2
  • Can you give sample data in table and sample output? Commented Jun 27, 2017 at 6:42
  • I have added sample data. Commented Jun 27, 2017 at 7:12

2 Answers 2

5

You can use foreign key mappings in the templates to reference objects.

Suppose you have two tables Table1 and Table2, with a mapping like Table1 -> Table2, with a foreign key field in Table2 referencing Table1, you can do something like this:

Your views.py would be something like this:

table1 = Table1.objects.all() 
table2 = Table2.objects.all() 
return render('template_name.html',{'table1':table1, 'table2':table2})

then your html template will be like:

{% for table1Instace in table1 %}
    
    {% for table2Instance in table1Instace.table2_set.all %}
    
    <!--Print table elements here-->
    
    {% endfor %}

{% endfor %}

where your table2Instance will be an objects of the row(s) in table2 corresponding to their foreign key mapping in table1.

Sign up to request clarification or add additional context in comments.

2 Comments

Two minor corrections to the above answer. May be the query for Table 2 is not required. Also in templates we cannot use () after 'all()'. This is similar to using '.items()' in view and simply 'items' in templates.
table1Instace.table2_set.all can i get first element from this list
1

models.py

class Category(models.Model):
    name = models.CharField(max_length=45, blank=True)

class Items(models.Model):
    name = models.CharField(max_length=45, blank=True)
    rate = models.FloatField(default=0)
    category_id = models.ForeignKey(Category)

views.py

def test_category(request):
    s = Category.objects.all()
    return_dict = {
        's' :   s        
    }
    return render(request, 'base.html', return_dict)

{% for item in s%}
    {{ item.name }}
    {% for x in item.items_set.all %}
        -- {{ x.name }}
    {% endfor %}
{% endfor %}

1 Comment

s must be queryset of Items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.