2012-07-13 2 views
0

하나의 앱과 2 개의 모델에 2 개의 모델이 있습니다. 나는 현재 완벽하게 1 모델에서 정보를 끌어오고있다. 그러나 나는 같은 응용 프로그램에서 다른 모델을 가져 와서 같은 페이지로 출력하려고합니다.다른 모델의 쿼리 세트 합치기

페이지의 아이디어는 뉴스 허브가되어 다른 유형의 뉴스 게시물 (한 모델에서)과 다른 유형의 게시물을 다른 모델에서 가져옵니다.

나는 장고에 대해 상당히 새롭기 때문에 쉽게 간다! 어쨌든 여기에 :) 코드입니다 :

// 뷰는

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 

// 모델이

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 내에서 출력 할 수 있다고 생각했지만 -posted 명령을 사용해야합니다. 그래서이 일이 엉망이 될 수 있습니다.

더 많은 정보가 필요하면 알려주십시오.

미리 감사드립니다.

+0

중복 : http://stackoverflow.com/questions/313137/using-django-how-can-i-combine-two-queries-from-separate-models-into-one-query – histrio

+0

이가요 실제로 내가해야 할 일은 무엇일까요? 그 게시물로 그들은 화제를 떠나고 최종 결과에 다른 무언가를하고있는처럼 보인다? – JDavies

+0

NewsPost에 DateTimeField가 표시되지 않습니다. 게시일 기준으로 어떻게 주문 하시겠습니까? – jpic

답변

1

이제 문제가 해결되었습니다.

news_hub = list(chain(notes, recent_posts)) 

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