I’m trying to go through this example and Now I’m getting
django.core.exceptions.FieldError: Unknown field(s) (pub_date, question_text) specified for Question
I was on https://docs.djangoproject.com/en/3.1/intro/tutorial07/
I’m trying to go through this example and Now I’m getting
django.core.exceptions.FieldError: Unknown field(s) (pub_date, question_text) specified for Question
I was on https://docs.djangoproject.com/en/3.1/intro/tutorial07/
Can you please post your models.py file and your admin.py file?
When posting code please surround your code in three back ticks like this
Not sure 3 backticks. I see Blockquote and preformatted text
indent preformatted text by 4 spaces
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 Question(models.Model):
# ...
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
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.choice_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
indent preformatted text by 4 spaces
from django.contrib import admin
from .models import Choice, Question
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_filter = ['pub_date']
list_display = ('question_text', 'pub_date', 'was_published_recently')
admin.site.register(Question, QuestionAdmin)
Is there a reason pub_date isn’t a field on your model? My gut instinct is telling me that the QuestionAdmin doesn’t know what pub_date is because it isn’t a model field.
My apologies, three backticks meaning ``` then your code, then three more backticks to end the code block.
To clarify - from the way you have your code commented, my guess would be that you interpreted that section of the tutorial to replace your Question model instead of adding those attributes to your model.
The commented elipses (…) in the tutorial for that class is intended to be a visual indicator that everything in that section remains the same, and that the lines of code below add to the existing code - or possibly modify it if it’s basically the same code.
This means your Question model should be the combination of the two Question models in what you posted, but as just the one model.