Django Meta Class - 将订购应用于用户的模型字段

时间:2017-05-16 13:40:58

标签: python-2.7 django-models django-rest-framework

我是Django 1.9的新手,我有一个models.py:

class MyProfile(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    Role = ArrayField(models.CharField(max_length=1000), blank=True,null=True)
    ContactNumber = models.CharField(max_length=100)

    class Meta:
        ordering = ('date_joined',)

在这里,我想使用date_joined的字段User model订购表格。

当我尝试运行python manage.py makemigrations时,它给出了如下错误:

SystemCheckError: System check identified some issues:

ERRORS:
projectmanagement.UniservedTeam: (models.E015) 'ordering' refers to the non-existent field 'date_joined'.

我如何实现这一目标?

1 个答案:

答案 0 :(得分:5)

设置通用API视图的排序:

http://www.django-rest-framework.org/api-guide/filtering/#orderingfilter

使用:^\/api\/([\w]+)\/([\w]+)\/([a-f0-9]{24})\/?\??.*$

编辑:

你可以像这样指定元类的排序:

ordering = ('user__date_joined', )