Python 如何将多个Django模型收集到一个列表中?

Python 如何将多个Django模型收集到一个列表中?,python,django,django-models,Python,Django,Django Models,我在Django有一个相当简单的博客,文章和链接有单独的模型。我希望在我的模板中有一个循环,以日期顺序列出它们,这意味着类似这样的内容: def listview(request): return render_to_response('index.dtmpl', { 'articles' : ArticlesAndLinks.objects.order_by('post_date')[:10] }, context_instance = RequestConte

我在Django有一个相当简单的博客,文章和链接有单独的模型。我希望在我的模板中有一个循环,以日期顺序列出它们,这意味着类似这样的内容:

def listview(request):
    return render_to_response('index.dtmpl', {
        'articles' : ArticlesAndLinks.objects.order_by('post_date')[:10]
    }, context_instance = RequestContext(request)
我不知道该怎么做。我是否必须分别抓取
文章.对象.order\u by('post\u date')
链接.objects.order\u by('post\u date')
,合并它们并重新排序?还是有更好的Django-ish/Pythonic方法来实现这一点


如果有帮助的话,Post和Links都是抽象类Post的子类,但由于它是一个抽象类,我似乎无法在其上运行集合。

结果表明,解决方案是将抽象类转换为真实类,然后我可以收集它。

结果表明,解决方案是将抽象类转换为真实类,然后我可以收集这些信息。

好吧,显而易见的答案是让Post成为一个具体的类。否则,您可能必须压缩ORM并求助于手工编码的SQL或手动合并/排序两个查询集。鉴于数据集的规模较小,我会选择最后一种解决方案。

好吧,显而易见的答案是将Post变成一个具体的类。否则,您可能必须压缩ORM并求助于手工编码的SQL或手动合并/排序两个查询集。鉴于数据集规模较小,我选择最后一种解决方案。

重构可能是更好的解决方案,但这里有另一种解决方案:

创建自定义管理器:

class PostManager(models.Manager):
    def mixed(self, first):
        all_dates = []
        articles_dates = Articles.objects.extra(select={'type':'"article"'}).values('id', 'post_date', 'type').order_by('-post_date')[:first]
        links_dates = Links.objects.extra(select={'type':'"link"'}).values('id', 'post_date', 'type').order_by('-post_date')[:first]
        all_dates.extend(articles_dates)
        all_dates.extend(links_dates)
        # Sort the mixed list by post_date, reversed
        all_dates.sort(key=lambda item: item['post_date'], reverse=True)
        # Cut first 'first' items in mixed list
        all_dates = all_dates[:first]
        mixed_objects = []
        mixed_objects.extend(Articles.objects.filter(id__in=[item['id'] for item in all_dates if item['type'] = 'article']))
        mixed_objects.extend(Links.objects.filter(id__in=[item['id'] for item in all_dates if item['type'] = 'link']))
        # Sort again the result list
        mixed_objects.sort(key=lambda post: post.post_date, reverse=True)
        return mixed_objects
并在抽象模型中使用它:

class Post(models.Model):

    class Meta:
        abstract = True

    objects = PostManager()
然后,对混合对象的调用将是:

Article.objects.mixed(10)

重构可能是更好的解决方案,但这里有另一个解决方案:

创建自定义管理器:

class PostManager(models.Manager):
    def mixed(self, first):
        all_dates = []
        articles_dates = Articles.objects.extra(select={'type':'"article"'}).values('id', 'post_date', 'type').order_by('-post_date')[:first]
        links_dates = Links.objects.extra(select={'type':'"link"'}).values('id', 'post_date', 'type').order_by('-post_date')[:first]
        all_dates.extend(articles_dates)
        all_dates.extend(links_dates)
        # Sort the mixed list by post_date, reversed
        all_dates.sort(key=lambda item: item['post_date'], reverse=True)
        # Cut first 'first' items in mixed list
        all_dates = all_dates[:first]
        mixed_objects = []
        mixed_objects.extend(Articles.objects.filter(id__in=[item['id'] for item in all_dates if item['type'] = 'article']))
        mixed_objects.extend(Links.objects.filter(id__in=[item['id'] for item in all_dates if item['type'] = 'link']))
        # Sort again the result list
        mixed_objects.sort(key=lambda post: post.post_date, reverse=True)
        return mixed_objects
并在抽象模型中使用它:

class Post(models.Model):

    class Meta:
        abstract = True

    objects = PostManager()
然后,对混合对象的调用将是:

Article.objects.mixed(10)