简体   繁体   English

Django通用外键排序

[英]Django Generic Foreign Key Sorting

So I have a model that has a generic foreign key relationship with with three other models 所以我有一个与其他三个模型具有通用外键关系的模型

class Status(BaseRequestStatus):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

class Request(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    ...some fields
    statuses = GenericRelation(Status, related_query_name='request')

class AnotherRequest(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    ...some fields
    statuses = GenericRelation(Status, related_query_name='another_request')

class ThirdRequest(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    ...some fields
    statuses = GenericRelation(Status, related_query_name='third_request')

So currently I am displaying the statuses with the name in a table. 所以目前我在表格中显示名称的状态。 I want to be able to sort by the name. 我希望能够按名称排序。 Currently I am sorting it like this. 目前我正在这样排序。

Status.objects.all().order_by('request__name', 'another_request__name', 'third_request__name')

however with this method all of the 'request' is sorted in a chunk, then 'another_request', then 'third_request' 但是使用这种方法,所有'request'都在一个chunk中排序,然后'another_request',然后'third_request'

is there anyway to sort all of these together? 无论如何要将所有这些整理在一起吗? I was able to do it by converting it to a list.. but in this case I need to use a Queryset. 我能够通过将其转换为列表来实现它。但在这种情况下,我需要使用Queryset。

Thanks in advance for any help. 在此先感谢您的帮助。

If you want them treated the same way (deducing this from the fact that you want them sorted by their common name field) then this indicates that these models have the same base. 如果您希望它们以相同的方式处理(从您希望它们按公共name字段排序的事实中推断出这一点),则表明这些模型具有相同的基础。

Use model inheritance in that case with a base model that is abstract and defines the name field and the generic fkey relation. 在这种情况下,使用模型继承与抽象的基本模型,并定义name字段和通用fkey关系。

class RequestBase(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    statuses = GenericRelation(Status, related_query_name='request')

    class Meta:
        abstract = True

class Request(RequestBase):
    # ... more fields

Status.objects.all().order_by('request__name')

A more general explanation: to be able to sort objects by one attribute, you have to make clear that they all share that same attribute. 更一般的解释是:为了能够按一个属性对对象进行排序,您必须明确它们共享同一个属性。 Either by fetching the values in your own routines and creating lists that are sortable (this would be the qs approach you mentioned) or by creating a common interface - which is this Model inheritance approach. 通过在您自己的例程中获取值并创建可排序的列表(这将是您提到的qs方法)或创建公共接口 - 这是此模型继承方法。

As long as you are not using table inheritance, though, you will not be able to use the database for sorting (meaning: you cannot use the Django ORM for sorting). 但是,只要您不使用表继承,就无法使用数据库进行排序(意思是:您不能使用Django ORM进行排序)。

There are certainly other approaches, including those that are outside of Django models: for example generating an additional lookup structure like a search index which will contain your transformed data as required (eg a haystack index). 当然还有其他方法,包括Django模型之外的方法:例如,生成一个额外的查找结构,如搜索索引,它将根据需要包含您的转换数据(例如,大海捞针索引)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM