合并来自不同模型的查询集

时间:2012-07-13 10:30:39

标签: django django-models django-views

我在一个应用程序和一个视图中有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中输出它们,但是它们需要通过-posted来命令。所以我虽然这可能搞砸了。

如果您需要更多信息,请告诉我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

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

news_hub = list(chain(notes, recent_posts))

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