Reputation: 195
I got this error when im coding in the second part of the django tutorial, i don't know why, i have the same code that the website.
Django 1.8.3
ERRORS:
<class 'polls.admin.ChoiceInline'>: (admin.E202) 'polls.Choice' has no ForeignKey to 'polls.Choice'.
System check identified 1 issue (0 silenced).
My models.py
import datetime
from django.db import models
from django.utils import timezone
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):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
My admin.py
from django.contrib import admin
from .models import Choice, Question
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None,{'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
admin.site.register(Choice, ChoiceInline)
admin.site.register(Question, QuestionAdmin)
I really will preciate the help, I really have no idea what is the problem , and want to finish this tutorial
Upvotes: 0
Views: 832
Reputation: 308999
The ChoiceInline
has already been included in your QuestionAdmin
by setting
inlines = [ChoiceInline]
.
This means that when you edit a question, you will be able to add, edit and delete that question's choices at the same time.
You are getting the error because of this line:
admin.site.register(Choice, ChoiceInline)
This is invalud, becayse you can't register a model with an Inline
. You can only register a model with a ModelAdmin
class. To stop the error, simply remove this line from your code.
If you want to edit choices by themselves, you need to define a ChoiceAdmin
class and register that.
admin.site.register(Choice, ChoiceAdmin)
Or, if you don't need any customisation, you don't actually need a model admin.
admin.site.register(Choice)
Upvotes: 2