Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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_Django Models - Fatal编程技术网

Python 基于返回值的Django模型排序

Python 基于返回值的Django模型排序,python,django,django-models,Python,Django,Django Models,我有一个Django模型,如下所示: class LegalEntityOwn(models.Model): # the fields class Meta: managed = False db_table = 'legal_entity_own' ordering = [... the returned value defined by me below...] def __unicode__(self):

我有一个Django模型,如下所示:

class LegalEntityOwn(models.Model): 
    # the fields 
    class Meta:
        managed = False
        db_table = 'legal_entity_own'
        ordering = [... the returned value defined by me below...]

    def __unicode__(self):
        if ....
            return x 
        else:
            return y

基本上,我的问题已在守则内说明。我想将默认顺序设置为基于unicode方法中定义的顺序。有什么方法可以做到这一点吗?

您能解释一下为什么要这样做吗?
\uuuu unicode\uuuu
方法用于生成对象的字符串表示形式。现在还不清楚你为什么要用它来确定订单。是的,当然。在Django表单小部件中呈现模型对象列表时,我希望combobox按照用户在combobox中看到的选项排序,但我仍然不确定我是否理解,但无论如何,我认为这是不可能的<代码>排序定义希望数据库引擎按其排序结果的数据库字段。数据库不知道您打算如何处理这些数据。如果您想基于某些生成的属性(如字符串表示)进行排序,则必须在获取结果后在Python中手动排序。您不能使用
\uuuuuu unicode\uu
方法进行排序。但是您可以读取组合框中的值,在您的视图中do
entities=LegalEntityOwn.objects.all().order\u by(组合框中的值)
您能解释一下为什么要这样做吗?
\uuuu unicode\uuuu
方法用于生成对象的字符串表示形式。现在还不清楚你为什么要用它来确定订单。是的,当然。在Django表单小部件中呈现模型对象列表时,我希望combobox按照用户在combobox中看到的选项排序,但我仍然不确定我是否理解,但无论如何,我认为这是不可能的<代码>排序定义希望数据库引擎按其排序结果的数据库字段。数据库不知道您打算如何处理这些数据。如果您想基于某些生成的属性(如字符串表示)进行排序,则必须在获取结果后在Python中手动排序。您不能使用
\uuuuuu unicode\uu
方法进行排序。但是您可以在组合框中读取值,在视图中do
entities=LegalEntityOwn.objects.all().order\u by(组合框中的值)