def was_published_recently(self): now = timezone.now() return now - datetime.timedelta(days=1) <= self.pub_date <= now
$ python manage.py test polls
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).
from django.contrib import adminfrom .models import Question, Choiceclass ChoiceInline(admin.TabularInline):model = Choiceextra = 3class 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)
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
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