Search code examples
djangodatabasemodels

Django Tutorial Step 2


I am at the shell part of the django tutorial I added the ____str____ method to the polls/models.py

here is my models.py:

from __future__ import unicode_literals
from django.db import models

# Create your models here.


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.question_text


class Question(models.Model):
    # ...
    def was_published_recently(self):

However when I run my server I get this error:

ERRORS:
polls.Choice.question: (fields.E300) Field defines a relation with model    'Question', which is either not installed, or is abstract.

Can someone show where I goofed up my models.py I cant find a completed example for that tutorial.


Solution

  • You shouldn't have two model classes named Question delete the second one and move the function into the first Question class.

    class Question(models.Model):
        question_text = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
    
        def __str__(self):
            return self.question_text
    
        def was published_recently(self):
            ##