Issue with Tutorial pt 5 "Fixing the bug"

64 views
Skip to first unread message

John Wall

unread,
Jan 23, 2017, 12:08:33 PM1/23/17
to Django users
Hello all,
I am working through the Django tutorial. I've made it to "Fixing the bug" within part 5, but am having issues. The tutorial shows the following lines:
def was_published_recently(self):
    now = timezone.now()
    return now - datetime.timedelta(days=1) <= self.pub_date <= now
to be placed in polls/models.py.

I have made these changes and re-run the test:
$ python manage.py test polls
 Which throws:
Creating test database for alias 'default'...
SystemCheckError: System check identified some issues:

ERRORS:
<class 'polls.admin.QuestionAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'was_published_recently', which is not a callable, an attribute of 'QuestionAdmin', or an attribute or method on 'polls.Question'.

System check identified 1 issue (0 silenced).
I have gone into admin.py, but have been unable to spot the error. My admin.py looks like:
from django.contrib import admin

from .models import Question, Choice

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]
class QuestionAdmin(admin.ModelAdmin):
list_display = ('question_text', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question_text']

admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice)

Does anyone have any recommendations as to what my issue might be?
Thanks,
John

Melvyn Sopacua

unread,
Jan 24, 2017, 6:18:40 AM1/24/17
to django...@googlegroups.com

On Monday 23 January 2017 08:22:28 John Wall wrote:

 

>

> class QuestionAdmin(admin.ModelAdmin):

> fieldsets = [

 

and whoops:

> class QuestionAdmin(admin.ModelAdmin):

 

You have 2, sir.

--

Melvyn Sopacua

John Wall

unread,
Jan 24, 2017, 10:45:50 AM1/24/17
to Django users
I'm guessing you mean that I have to class calls for QuestionAdmin? I was not able to get the code from the tutorial to work any other way. Might it be some issue with my version of Python?

Tim Graham

unread,
Jan 24, 2017, 2:14:58 PM1/24/17
to Django users
Correct, you shouldn't declare class QuestionAdmin(...) twice. What problem do you see if you combine those classes? It's unlikely related to your Python version.

Melvyn Sopacua

unread,
Jan 29, 2017, 9:43:10 PM1/29/17
to django...@googlegroups.com

On Tuesday 24 January 2017 11:14:58 Tim Graham wrote:

> Correct, you shouldn't declare class QuestionAdmin(...) twice. What

> problem do you see if you combine those classes? It's unlikely

> related to your Python version.

>

> On Tuesday, January 24, 2017 at 10:45:50 AM UTC-5, John Wall wrote:

> > I'm guessing you mean that I have to class calls for QuestionAdmin?

 

Note that Tim calls it "declare" and you call it "calls". Not being pedantic, but it's the key to understanding your mistake: You can only declare something once in the same scope. You can call it as many times as you want.

 

The scope is also important:

 

class Awesome(models.Model):

name = models.CharField(max_length=200)

 

def awesome_factory(unique=False):

class Awesome(models.Model):

name = models.CharField(max_length=200, unique=unique)

class Meta:

abstract = True

return Awesome

 

class TheBomb(awesome_factory(unique=True)):

pass

 

class Magnificent(awesome_factory()):

pass

 

As you see, I declared two Awesome classes, but the second is in the scope of the awesome_factory function - so this works.

 

The result is that in the outer scope Awesome and Magnificent are identical models with a different name and that TheBomb has a name field that enforces uniqueness.

 

Hope this sheds some light on things :)

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages