简体   繁体   English

Django 1.6获取fk对象

[英]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 . 我想将它们加入到一个视图中,当有人输入此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. 显然,该ID将取决于Project.id,并且它必须获取与其相关的所有注释。

Meanwhile my views.py looks like this 同时我的views.py看起来像这样

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. 除非您用一些信息填充project_note_list ,否则将没有任何要迭代的内容。

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. 借助ForeignKey ,并且具有多对一关系,project对象将具有与之关联的一组注释,而不必查找它们。

You should be able to query them using project.note_set.all() or in your template by using: 您应该能够使用project.note_set.all()或在模板中通过以下方式查询它们:

{% for note in project.note_set %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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