Kyle Sponable
Kyle Sponable

Reputation: 735

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.

Upvotes: 1

Views: 185

Answers (2)

Ashok Joshi
Ashok Joshi

Reputation: 448

You have created 2 models with name Question. So please remove the second one.It creates a collision thst's why the Choice class can't find the Question Model.

Upvotes: 0

kaykae
kaykae

Reputation: 731

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):
        ##

Upvotes: 1

Related Questions