2012-07-13 60 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] 
相關問題