我在models.py中的python django中运行此命令python manage.py makemigrations blog时出错

时间:2017-02-12 11:51:16

标签: python django postgresql

我在python manage.py makemigrations blog的{​​{1}}中运行此命令python django时出现错误

models.py

1 个答案:

答案 0 :(得分:0)

您的代码存在以下几个问题:

  • 压痕
  • 用户类导入
  • 使用Blog代替blog.Blog

快速修复:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User


class Post(models.Model):
    author = models.ForeignKey(User)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title


class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments')
    author = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    approved_comment = models.BooleanField(default=False)

    def approve(self):
        self.approved_comment = True
        self.save()`enter code here`

    def __str__(self):
        return self.text