Reputation: 189
I'm new to Python and especially to Django. While trying to dive in subj. framework, and run through its official tutorial I got some pain in the neck error which says:
Attribute error: 'Poll' object has no attribute 'was_published_recently'
I type the next in django shell (invoked by: "python manage.py shell" from projects directory):
>>> from polls.models import Poll, Choice
>>> from django.utils import timezone
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()
and I get the next shell output:
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'Poll' object has no attribute 'was_published_recently'"
Can someone help me to get what am I doing wrong here? Because I just have no idea what can lead to such an error... (Already googled inside out the question, but didn't find an answer that could solve my situation).
I use:
Django version 1.5.1
Python version 2.7.5
Here're my "Poll" model code:
import datetime
from django.utils import timezone
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text
Also, here's my "Admin" file:
from django.contrib import admin
from polls.models import Choice, Poll
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question', 'pub_date', 'was_published_recently')
admin.site.register(Choice)
admin.site.register(Poll, PollAdmin)
Upvotes: 2
Views: 1534
Reputation: 9
I think that all it is saying is that you haven't included any was_published_recently function in the class. Thanks for including the admin.py & polls.py files, but I think it's in your models.py file that you need to ensure a couple things. It looks like you need to make sure that
from django.utils import timezone
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
are included in your models.py file.
Upvotes: 0
Reputation: 1619
make sure you use 4 spaces as indentation instead Tab character, tab makes function does not recognize.
Upvotes: 2