订购Django 1.9模型时出错如何解决这个问题?

时间:2017-02-22 14:11:21

标签: python django

来自CMD的输出:

  

django.core.management.base.SystemCheckError:SystemCheckError:系统检查发现了一些问题:

     

错误:posts.Comments :( models.E015)'ordered'指的是   不存在的字段'timestamp'。 posts.Comments :( models.E015)   “排序”是指不存在的字段“已更新”。

     

系统检查确定了2个问题(0个沉默)。

当前代码:

from __future__ import unicode_literals
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import pre_save
from django.utils import timezone

from django.utils.text import slugify


class PostManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(PostManager, self).filter(draf=False).filter(publish__lte=timezone.now())

def upload_location(instance, filename):
    return "%s/%s" %(instance.id, filename)

class Post(models.Model):
    user                    = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    title                   = models.CharField(max_length=70)
    slug                    = models.SlugField(unique=True)
    image                   = models.ImageField(upload_to=upload_location,
                                                null=True,
                                                blank=True, 
                                                width_field="width_field", 
                                                height_field="height_field")
    width_field             = models.IntegerField(default=0)
    height_field            = models.IntegerField(default=0)
    content                 = models.TextField(max_length=220)
    draf                    = models.BooleanField(default=False)
    publish                 = models.DateField(auto_now=False, auto_now_add=False)
    updated                 = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp               = models.DateTimeField(auto_now=False, auto_now_add=True)

class Comments(models.Model):
    class Meta:
        db_table        = 'comments'

    comments_text       = models.CharField(max_length=120)
    comments_posts      = models.ForeignKey(Post)

    objects = PostManager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})
        #return "/posts/%s/" %(self.id)

    class Meta:
        ordering = ["-timestamp", "-updated"]

def create_slug(instance, new_slug=None):
    slug = slugify(instance.title)
    if new_slug is not None:
        slug = new_slug
    qs = Post.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
        return create_slug(instance, new_slug=new_slug)
    return slug

def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)

pre_save.connect(pre_save_post_receiver, sender=Post)

2 个答案:

答案 0 :(得分:0)

模型评论没有以下字段

 class Meta:
        ordering = ["-timestamp", "-updated"]

答案 1 :(得分:0)

您在timestamp模型中的字段updatedComments上订购了哪些不存在,而不是相关的Post模型。正如你的错误所说:

  

'ordered'是指不存在的字段'timestamp'   “排序”是指不存在的字段“已更新”。

您可以使用双下划线__过滤相关的模型字段: relationfield__fieldname

class Meta:
    ordering = ["-comments_posts__timestamp", "-comments_post__updated"]