I am facing some problems with model.py file
the code of model.py is:
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
when i am run this command python manage.py shell the following error occurs
File "/home/ghrix/testing/demo/polls/models.py", line 9
def __unicode__(self):
^
IndentationError: unindent does not match any outer indentation level
this error occur after i add some lines of code in my model.py file
import datetime from django.utils import timezone
In Poll class
def __unicode__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
In Choice class
def __unicode__(self):
return self.choice
Python is very sensitive to indentation. Your particular code should look like this:
Remember, the suggested indentation is 4 spaces for each level. The problem you’re having is that your attributes like say
Poll.questionshould be defined at the same level as the method__unicode__.