Django:按元组的第一个值排序 [英] Django: Ordering by the first value of a tuple

查看:53
本文介绍了Django:按元组的第一个值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 7 元组元组:

I have a 7-tuple of tuples as such:

POSSIBILITIES = ((1, "Something"),
                 (2, "Something else"), ...)

现在我有一个带有 choicesIntegerField 和上面列出的可能性的模型.

Now I have an IntegerField with choices in a model with the possibilities listed above.

class Something(models.Model):

    class Meta:
        ordering = "...?"

    something = models.IntegerField(choices=POSSIBILITIES)

我希望数据库中的条目默认按每个元组中的整数排序.我该如何指定?

I want the entries in the database to be ordered by the integer in each of the tuples by default. How do I specify that?

推荐答案

这应该可以解决问题:

class Meta:
    ordering = ('something',)

最后一个逗号很重要,它是必需的.

The last comma is important, it is required.

这篇关于Django:按元组的第一个值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆