0

我在使用model.py文件时遇到了一些问题

model.py 的代码是:

from django.db import models

import datetime
from django.utils import timezone

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
 def __unicode__(self):
        return self.question
 def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
 def __unicode__(self):
        return self.choice

当我运行这个命令python manage.py shell时出现以下错误

File "/home/ghrix/testing/demo/polls/models.py", line 9
    def __unicode__(self):
                         ^
IndentationError: unindent does not match any outer indentation level

在我的 model.py 文件中添加一些代码行后发生此错误

从 django.utils 导入日期时间

在投票类

def __unicode__(self):
        return self.question
 def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

在选择课

def __unicode__(self):
        return self.choice
4

5 回答 5

2

Python 对缩进非常敏感。您的特定代码应如下所示:

from django.db import models

import datetime
from django.utils import timezone

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.question

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

    def __unicode__(self):
        return self.choice

请记住,建议的缩进是每个级别 4 个空格。您遇到的问题是您的属性如 sayPoll.question应该定义在与 method 相同的级别__unicode__

于 2012-08-23T06:30:52.973 回答
0

看起来您的代码有缩进错误。试试这个:

class Choice(models.Model):
  poll = models.ForeignKey(Poll)
  choice = models.CharField(max_length=200)
  votes = models.IntegerField()

  def __unicode__(self):
      return self.choice

类也是一样Pool

于 2012-08-23T06:30:36.843 回答
0

似乎您可能将空格与制表符混合在一起,请尝试运行:

python -m tabnanny models.py

并修复制表符/空格缩进错误。根据PEP8 ,始终使用空格而不是制表符是个好主意。

于 2012-08-23T06:31:19.667 回答
0

请改用以下文件。你有缩进问题。请记住,同一级别的所有代码都应正确对齐。

您还应该确保不要在编辑器中混用空格和制表符。一般来说,最好坚持使用空格。关键是确保你不要混搭——否则你会得到类似的错误。

import datetime

from django.db import models
from django.utils import timezone

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return unicode(self.question)

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

    def __unicode__(self):
        return unicode(self.choice)
于 2012-08-23T06:34:08.217 回答
0

def __unicode__(self):缩进小于andquestion =pub_date =。如果它是类的def一部分,则应该与类中它上面的其余代码具有相同的缩进。

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    # the def should start at this indent level like the lines above
    # <<<<<<<<
    def __unicode__(self):
        return self.question
于 2012-08-23T06:34:14.597 回答