Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:来自不同模型的模型实例的有序列表?_Python_Django_Sorting_Django Models_Django Queryset - Fatal编程技术网

Python Django:来自不同模型的模型实例的有序列表?

Python Django:来自不同模型的模型实例的有序列表?,python,django,sorting,django-models,django-queryset,Python,Django,Sorting,Django Models,Django Queryset,用户可以将三种不同类型的内容上传到我们的网站:图像、视频、音频。以下是每种类型的模型: class ImageItem(models.Model): user = models.ForeignKey(User) upload_date = models.DateTimeField(auto_now_add=True) image = models.ImageField(upload_to=img_get_file_path) title = models.Char

用户可以将三种不同类型的内容上传到我们的网站:图像、视频、音频。以下是每种类型的模型:

class ImageItem(models.Model):
    user = models.ForeignKey(User)
    upload_date = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to=img_get_file_path)
    title = models.CharFiled(max_length=1000,
                             blank=True)

class VideoItem(models.Model):
    user = models.ForeignKey(User)
    upload_date = models.DateTimeField(auto_now_add=True)
    video = models.FileField(upload_to=vid_get_file_path)
    title = models.CharFiled(max_length=1000,
                             blank=True)

class AudioItem(models.Model):
    user = models.ForeignKey(User)
    upload_date = models.DateTimeField(auto_now_add=True)
    audio = models.FileField(upload_to=aud_get_file_path)
    title = models.CharFiled(max_length=1000,
                             blank=True)
我有一个名为
library.html
的页面,它按照从最近上传到最早上传的顺序呈现用户上传的所有项目(它显示每个实例的
标题
上传日期
,并在左侧放置一个小图标,表示它是什么类型的项目)


假设它需要三个独立的查询,如何合并这三个查询集?如何确保它们与最近上传的内容顺序一致?

attrgetter
可用于提取您可能希望按其排序的对象的属性

from operator import attrgetter

results = []
results.extend(list(AudioItem.objects.filter(...)))
results.extend(list(VideoItem.objects.filter(...)))
results.extend(list(ImageItem.objects.filter(...))
results.sort(key=attrgetter("upload_date")

作为替代方案,您可以使用公共属性并将其因子化到超类模型中。然后,您只需在超类模型上按上传日期
order\u即可。第三方应用程序提供了一个名为继承管理器的自定义管理器,允许您自动向下转换到查询中的子类模型

from model_utils.managers import InheritanceManager

class MediaItem(models.Model):
    objects = InheritanceManager()

    user = models.ForeignKey(User)
    upload_date = models.DateTimeField(auto_now_add=True)
    title = models.CharFiled(max_length=1000,
                             blank=True)

class ImageItem(MediaItem):
    image = models.ImageField(upload_to=img_get_file_path)

class VideoItem(MediaItem):
    video = models.FileField(upload_to=vid_get_file_path)

class AudioItem(MediaItem):
    audio = models.FileField(upload_to=aud_get_file_path)
那么,您的查询就是:

MediaItem.objects.all().order_by('upload_date').select_subclasses()
这样,只需一个查询(使用3个连接)即可获得所需的内容。另外,您的数据得到了更好的规范化,并且支持各种更复杂的查询以及分页非常简单


即使你不这么做,我仍然会用它在概念上规范化你的数据模型,即使你没有得到数据库端和ORM的好处。

参考这个问题,这正是我使用的解决方案。受monkut的回答和Frantzdy评论中链接的顶级答案的影响(谢谢大家)


[见这里][1]看看这个答案:)订购应该不会太难。[1] :这绝对是做这件事的方法。如果将来需要更多的媒体类型,维护起来就容易多了。此外,它还具有良好的面向对象编程的重用特性。看来他的编辑尝试被第三方mods拒绝了,但我想在应该得到的地方给予表扬,因为我不知道如何覆盖和接受编辑。
from itertools import chain
from operator import attrgetter

images = ImageItem.objects.filter(user=user)
video = VideoItem.objects.filter(user=user)
audio = AudioItem.objects.filter(user=user)
result_list = sorted(chain(images, video, audio),
                     key=attrgetter('upload_date'),
                     reverse=True)