-1

I'm just learning django part time and I'm stuck on a problem that I just can't seem to find a solution to. I feel it is probably pretty simple, but I just can't seem to see it right now.

In django I have the following table

class category(models.Model):
    category = models.CharField(max_length=100)

    def __str__(self):
        return f"{self.id} is {self.category}"

class knowledge(models.Model):
    name = models.CharField(max_length=16)
    location = models.URLField(max_length=1024)
    title = models.TextField(blank=True, max_length=512)
    description = models.TextField(max_length=1024)
    category = models.ForeignKey(category, on_delete=models.CASCADE, related_name="content")

    def __str__(self):
        return f"{self.id}{self.name} {self.location} {self.description}"

class notes(models.Model):
    note = models.TextField(blank=True, max_length=1024, default='none')
    date_added = models.DateTimeField(blank=True, default=timezone.now)
    knowledge = models.ForeignKey(knowledge, on_delete=models.CASCADE, related_name="mynotes")

    def __str__(self):
        return f"Added on {self.date_added}"

For my views I have

def kbs(request, category_id):
    # get all the objects in the category table with the id specified
    cate = category.objects.get(id=category_id)
    # once you have the category id get all the objects from the knowledge table using the related name content
    know = cate.content.all()

    # for each id in know create a list of notes?

    return render(request, "knowledge/category.html", {"cate": cate, "know": know,})

I can loop over all the items in the knowledge table and display them in my template. I wanted to add some notes to each knowledge article from another table. I added the notes table and referenced the knowledge table. In the template instead of displaying the id, I would like to loop over all the notes associated with that id and list those notes there.

here is my template

{% for knowledge in know %}
        <h5{{knowledge.title}}</h5>
        <p>{{ knowledge.description|linebreaks }}</p>
        <p> {{ knowledge.id }}</p>
        <a href="{{ knowledge.location }}">{{knowledge.name}}</a>
   {% endfor %}

I imagine there are two things that I need to do. I need to pass in a list of all the notes to my template from my views, then when it comes to the id I should be able to use templating to list all the items in my notes for that particular knowledge id. Perhaps a for loop nested in the main loop. I'm sure there must be an easy solution, I just can't seem to be able to figure it out. Can anyone assist?

Sohaib
  • 566
  • 6
  • 15
Kripke
  • 1
  • 2
  • you might be looking for this [nested loops in templating](https://stackoverflow.com/questions/13232320/nested-loop-in-django-template) – Hemant Jan 02 '21 at 21:21

1 Answers1

0

Use the value of the related_name attribute to get the ForeignKey values of an object, in your template do something like this:

{% for knowledge in know %}
    {{ knowledge }}
    {% for note in knowledge.mynotes.all %} # btw I recommend renaming the 'mynotes' to something that is easier to understand like just 'notes'
        {{ note }}
    {% endfor %}
{% endfor %}
SLDem
  • 2,065
  • 1
  • 8
  • 28