2012-07-13 61 views
0

我在一个应用程序和1个视图中有2个模型。我目前正在从1个模型中获取信息,完全没问题。不过,我希望从同一个应用中获取另一个模型,并将它们输出到同一页面。合并来自不同模型的查询集

页面的想法是它成为一个新闻中心,所以它通过不同类型的新闻帖子(来自一个模型)和来自另一个模型的不同类型的帖子。

我对Django相当陌生,所以很简单! :)反正这里是代码:

// VIEWS

def news_home(request): 

    page_context = details(request, path="news-hub", only_context=True) 

    recent_posts = NewsPost.objects.filter(live=True, case_study=False).order_by("-posted")[:5] 

    recent_posts_pages = Paginator(recent_posts, 100) 

    current_page = request.GET.get("page", 1) 

    this_page = recent_posts_pages.page(current_page) 

    notes = BriefingNote.objects.filter(live=True).order_by("-posted") 

    news_categories = NewsCategory.objects.all() 




    news_context = { 
     "recent_posts": this_page.object_list, 
     "news_categories": news_categories, 
     "pages": recent_posts_pages, 
     "note": notes, 


    } 

    context = dict(page_context) 
    context.update(news_context) 


    return render_to_response('news_hub_REDESIGN.html', context, context_instance=RequestContext(request)) 

//模型1

class BriefingNote(models.Model): 
    title = models.CharField(max_length=300) 
    thumbnail = models.ImageField(upload_to='images/briefing_notes', blank=True) 
    file = models.FileField(upload_to='files/briefing_notes') 
    live = models.BooleanField(help_text="The post will only show on the frontend if the 'live' box is checked") 
    categories = models.ManyToManyField("NewsCategory") 

    # Dates 
    posted = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 

    def __unicode__(self): 
     return u"%s" % self.title 

//模型2

class NewsPost(models.Model): 
    title = models.CharField(max_length=400) 
    slug = models.SlugField(help_text="This will form the URL of the post") 

    summary = models.TextField(help_text="To be used on the listings pages. Any formatting here will be ignored on the listings page.") 
    post = models.TextField(blank=True) 
    #TO BE REMOVED???? 
    thumbnail = models.ImageField(help_text="To be displayed on listings pages", upload_to="images/news", blank=True) 
    remove_thumbnail = models.BooleanField() 

我输出内容在前端像这样:

{% for post in recent_posts %} 





        <div class='news_first'> 

         <img class="news_thumb" src="/media/{% if post.article_type %}{{post.article_type.image}}{% endif %}{% if post.news_type %}{{post.news_type.image}}{% endif%}" alt=""> 
         <h3><a href='{{post.get_absolute_url}}'>{% if post.article_type.title %}{{post.title}}{% endif %} <span>{{post.posted|date:"d/m/y"}}</span></a></h3> 
         <p class='news_summary'> 
          {% if post.thumbnail %}<a href='{{post.get_absolute_url}}'><img src='{% thumbnail post.thumbnail 120x100 crop upscale %}' alt='{{post.title}}' class='news_thumbnail'/></a>{% endif %}{{post.summary|striptags}} <a href='{{post.get_absolute_url}}'>Read full story &raquo;</a> 
         </p> 
         <div class='clearboth'></div> 
        </div> 




      {% endfor %} 

我在想也许我可以在同一个forloop中输出它们,但是他们需要通过发布命令。所以我尽管这可能会搞砸了。

如果您需要更多的信息,请让我知道。

在此先感谢。

+0

重复:http://stackoverflow.com/questions/313137/using-django-how-can-i-combine-two-queries-from-separate-models-into-one-query – histrio 2012-07-13 11:50:15

+0

这是实际上我需要做什么?作为那篇文章,他们脱离主题,似乎他们在最终结果上做了不同的事情? – JDavies 2012-07-13 11:51:54

+0

我在NewsPost中看不到DateTimeField,您打算如何通过发布日期来订购它们? – jpic 2012-07-13 11:56:30

回答

1

我现在已经解决了这个问题。

news_hub = list(chain(notes, recent_posts)) 

news_hub = sorted(
    chain(notes, recent_posts), 
    key = attrgetter('posted'), reverse=True)[:10] 
相关问题