5

I have a little problem which I can't seem to get my head around. It seems like a very trivial thing but I just can't figure it out. Essentially what I'm trying to do is to create a Project model which holds information on a certain project and then have another model called Link which holds the name of the link such as 'Get Program' or 'Download Release' and have a URLField which holds the URL for those links and is child of Project.

So far my models.py is as below:

from django.db import models class Project(models.Model): name = models.CharField(max_length=255) description = models.TextField() def get_name(self): return self.name def get_description(self): return self.description def __str__(self): return self.name class Link(models.Model): project = models.ForeignKey(Project) name = models.CharField(max_length=255) url = models.URLField() def get_name(self): return self.name def __str__(self): return self.name 

Problem is in my views.py I want to be able to pass Projects object so I can iterate over them and display all links for each project including the project objects fields such as name and description. So far I've done:

from django.shortcuts import render from django.http import HttpResponse from .models import Project, Link def projects_index(request): projects = Project.objects.all() links = Link.objects.all() context = { 'projects': projects, } return render(request, 'projects.html', context) 
2
  • What does the rest of the view function look like? Commented Sep 12, 2016 at 12:21
  • @JackEvans I've updated the views.py in the question Commented Sep 12, 2016 at 12:23

2 Answers 2

3

Inside your template projects.html you should be able to loop over the Links for each project with

{% for project in projects %} {% for link in project.link_set.all %} {{ link }} {% endfor %} {% endfor %} 
Sign up to request clarification or add additional context in comments.

4 Comments

Great! that did the trick, I can't believe it was within the loops. I feel stupid :/
No problem you might want to follow along with docs.djangoproject.com/en/1.10/intro/tutorial03/… if you get stuck in future
Does the project.link_set.all get all objects related to that instance of a model? so as an example If I had an object called morse, and I had links that were connected to morse, everything that is related to morse will be displayed?
Yes but each addtional {{ link.morse.thing }} will result in another duplicate Database Query. If you want to fetch all the related objects at once without duplicating queries you'll need to use select_related() and prefetch_related() as specified here docs.djangoproject.com/en/1.10/ref/models/querysets/…
0

You really should do the tutorial.

for project in projects: for link in project.link_set.all: <...> 

Add a related_name="links" to your foreign key, and the inner loop can be

for link in project.links: 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.