Django tutorial: unexpected indent error
Question: Here is my model.py code :

    
    
    from django.db import models
    # Create your models here.
    
    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
            def __str__(self):
            return self.question
    
    
    class Choice(models.Model):
        poll = models.ForeignKey(Poll)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
            def __str__(self):
            return self.choice_text
    

And when I run Following command:

    
    
    python manage.py runserver
    

this gives me following error:

> mjrulesamrat@mjrulesamrat-Lenovo-G570:~/django_local/first_web$ python
> manage.py runserver Validating models...
>
> Unhandled exception in thread started by Traceback (most recent call last):
> File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py",
> line 93, in wrapper fn(*args, **kwargs) File "/usr/local/lib/python2.7/dist-
> packages/django/core/management/commands/runserver.py", line 98, in
> inner_run self.validate(display_num_errors=True) File
> "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
> line 310, in validate num_errors = get_validation_errors(s, app) File
> "/usr/local/lib/python2.7/dist-
> packages/django/core/management/validation.py", line 34, in
> get_validation_errors for (app_name, error) in get_app_errors().items():
> File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py",
> line 196, in get_app_errors self._populate() File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line
> 75, in _populate self.load_app(app_name, True) File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line
> 99, in load_app models = import_module('%s.models' % app_name) File
> "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40,
> in import_module **import**(name) File
> "/home/mjrulesamrat/django_local/first_web/polls/models.py", line 7 def
> **str**(self): ^ IndentationError: unexpected indent

I'm using Django 1.6 and python 2.7.

Please guide me if i have made some mistake in this code. Cause when i run
follow in python shell it gives me poll object not the question.

    
    
    >>> Poll.objects.all()
    [<Poll: Poll object>]
    


Answer: Watch/Fix your indentation on the model method level:

    
    
    from django.db import models
    # Create your models here.
    
    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
    
        # HERE 
        def __str__(self):
            return self.question
    
    
    class Choice(models.Model):
        poll = models.ForeignKey(Poll)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
    
        # AND HERE
        def __str__(self):
            return self.choice_text
    

