打印字段仅与django中的foreignkey相关

时间:2015-05-21 23:04:54

标签: django foreign-keys

我正在尝试仅打印与我的模型的外键关联的条目。我有一系列列表,我想打印列表名称和该列表下的所有项目。我当前的问题是每个列表都有一个div生成,这很好,但是与每个列表关联的每个项目都在div内部打印出来,而不仅仅是与listname相关联的项目。

浏览

def control_panel(request, username):

    context = RequestContext(request)
    if username == request.user.username:
        if request.user.is_authenticated():
            user = User.objects.get(username=username)

            lists = request.user.newlist_set.all()

            listitems = request.user.newlistitem_set.all().filter(list_name = lists)

            return render_to_response('controlpanel.html', {'lists': lists,'listitems':listitems,}, context)
        else:
            return render_to_response('login.html', {}, context)
    else:
        return render_to_response('controlpanel.html', {}, context)

模型

from django.db import models
from django.contrib.auth.models import User

class newlist(models.Model):
    user = models.ForeignKey(User)
    list_name = models.CharField(max_length = 100)
    picture = models.ImageField(upload_to='profiles/', default = "")

    def __str__(self):
        return self.list_name

class newlistitem(models.Model):
    user = models.ForeignKey(User)
    list_name = models.ForeignKey(newlist)
    list_item = models.CharField(max_length = 200)


    def __str__(self):
        return self.list_item

HTML

            {% for lista in lists %} <!-- This is a call to the lists that exist for eaach model taht s instantiates -->

                    <div id = "indivlistitem">


                        <b>{{lista}}</b><br> <!-- This is the title of each list that is rendered to the HTML -->

                        {% for eachlistitem in listitems %}

                            {{eachlistitem}}

                        {%endfor%}

                    </div>

                {% endfor %}

2 个答案:

答案 0 :(得分:1)

根据我的理解,您不需要在您的上下文中使用您的列表项,因为新列表应该足够了。您也可以访问模板中的查询,因此您可以直接在模板中访问外键。像这样:

{% for eachlistitem in lista.newlistitem_set.all %}

。 这将为您提供当前lista为list_name的所有列表项

答案 1 :(得分:1)

问题在于你的models.py中的listitems定义。具体来说,您将lists定义为与给定用户相关的所有列表,这很好,但随后将listitems定义为与该用户相关的每个列表(filter(list_name=lists)相关的每个列表项,这很糟糕。

由于请求上下文只是python词典,并且可以嵌套,我会做类似以下的事情:

def control_panel(request, username):

    context = RequestContext(request)
    if username == request.user.username:
        if request.user.is_authenticated():
            user = User.objects.get(username=username)

            lists = request.user.newlist_set.all()

            listitems = {}
            for list in lists:
                listitems[list.list_name] = request.user.newlistitem_set.filter(list_name=list)

            return render_to_response('controlpanel.html', {'lists': lists,'listitems':listitems,}, context)
        else:
            return render_to_response('login.html', {}, context)
    else:
        return render_to_response('controlpanel.html', {}, context)

这将创建一个字典,每个列表有一个条目,然后您可以在模板中呈现:

        {% for lista in lists %} <!-- This is a call to the lists that exist for eaach model taht s instantiates -->

                <div id = "indivlistitem">


                    <b>{{lista}}</b><br> <!-- This is the title of each list that is rendered to the HTML -->

                    {% for eachlistitem in listitems.{{lista.list_name}} %}

                        {{eachlistitem}}

                    {%endfor%}

                </div>

            {% endfor %}