2015-05-21 31 views
0

我想只打印与我的模型的外键关联的条目。我有一系列的列表,我想打印列表名称和属于该列表的所有项目。我当前的问题是,每个列表都有一个div生成,这很好,但是与每个列表关联的每个项目都在div内打印出来,而不仅仅是与listname关联的项目。打印字段只与django中的foreignkey有关

查看

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 %} 

回答

1

据我所知,你不需要在你的情况下你的listItems中,具有newlists应该足够了。您也可以访问模板中的查询,因此您可以直接在模板中访问外键。像这样:

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

。 这会给你列出所有有lista列表当前lista列表名称

+0

我相信这就是我要做的,但是你的代码片段无法打印任何东西。 – ryan

+0

哼,我没有把正确的模型名称。它应该是lista.newlistitem_set.all。 –

+0

非常感谢! – ryan

1

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

由于请求上下文只是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 %} 
+0

嗯,我收到以下内容:异常类型:\t类型错误 异常值:\t all()获得了意外的关键字参数'list_name' – ryan

+0

对不起,all()应该是过滤器。上面更新了答案。 – JSchwerberg

+0

感谢您的解释,遗憾的是JulienGrégoire很好地解决了这个问题。 – ryan

相关问题