【问题标题】:Django can't ordering model object to ascending or descendingDjango 无法将模型对象排序为升序或降序
【发布时间】:2021-11-16 05:29:58
【问题描述】:

我在尝试订购我的模型对象时收到此错误。我的控制台错误:

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
contact.Contact: (models.E014) 'ordering' must be a tuple or list (even if you want to order by only one field).

System check identified 1 issue (0 silenced).

这是我的模型:

    class Contact(MPTTModel):
           user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True,related_name='contact_user')
           name = models.CharField(max_length=250)
           email = models.EmailField(max_length=500)
           subject = models.CharField(max_length=2000,blank=True,null=True)
           message = models.TextField()
           created_at = models.DateTimeField(auto_now_add=True,blank=True,null=True)
           updated_at = models.DateTimeField(auto_now=True,blank=True,null=True)
           
           class Meta:
                 ordering = ('-created_at')

我在哪里做错了?

【问题讨论】:

标签: python-3.x django django-models


【解决方案1】:

这是正确的语法,注意Meta 类中的comma

class Contact(MPTTModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True,related_name='contact_user')
    name = models.CharField(max_length=250)
    email = models.EmailField(max_length=500)
    subject = models.CharField(max_length=2000,blank=True,null=True)
    message = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True,blank=True,null=True)
    updated_at = models.DateTimeField(auto_now=True,blank=True,null=True)
           
    class Meta:
        ordering = ('-created_at',)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多