简体   繁体   中英

Django 1.6 getting fk objects

I have a this models

class Project(models.Model):
    name = models.CharField(max_length=20)
    description = models.CharField(max_length=200, null=True, blank=True)
    creation_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    group_owner = models.ForeignKey(User)

    def __str__(self, y):
        return smart_str(self.name)

class Note(models.Model):
    title = models.CharField(max_length=50, null=True, blank=True)
    content = models.CharField(max_length=1000)
    creation_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated_date = models.DateTimeField(auto_now_add=False, auto_now=True)
    projectId = models.ForeignKey(Project, on_delete=models.CASCADE)
    userid = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return smart_str(self.title)

which i would like to join them in one view, which would be shown when someone enters to this url http://localhost.com:8000/project/ID . obviously the ID will depend in Project.id and it will have to get all the Notes that are related to it.

Meanwhile my views.py looks like this

class ProjectDetail(generic.DetailView):
    model = Project
    template_name = 'scribere/project/detail.html'

    def get_queryset(self):
        return Project.objects.filter(group_owner__exact=self.request.user.id)

class ProjectNoteList(generic.ListView):
    context_object_name = 'project_note_list'
    template_name = 'scribere/note/index.html'

    def get_queryset(self):
        return Note.objects.filter(userid__exact=self.request.user.id, projectId__exact=self.kwargs['pk'])

and the template

{% extends "base/adminsNavBar.html" %}
{% load i18n %}

{% block head_title %}{% trans 'Projects Details' %}{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumb"><p>{% trans 'Project Details' %}</p></div>
{% endblock %}

{% block content %}
<div class='row'>
    <a href="{% url 'scribere:project_list' %}" class="btn btn btn-primary pull-right" role="button">{% trans 'Back' %}</a>
    <a href="{% url 'scribere:project_note_list' project.id %}" class="btn btn btn-primary pull-right" role="button">{% trans 'Notes' %}</a>
</div>
<div>
{{project.name}}
</div>
<div class = "note-list">
     <ul>
        {% for note in project_note_list %}
            <li>
                <a href= "#"> {{note}} </a>
            </li>
        {% endfor %}
    </ul>
</div>
{% endblock %}

which doesn't load any information about the note. What am i missing?

Unless you are populating project_note_list with some information, there will be nothing to iterate over.

By virtue of the ForeignKey s, and having a Many to one relation the project object will have a set of notes associated with it, without having to look them up.

You should be able to query them using project.note_set.all() or in your template by using:

{% for note in project.note_set %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM