indentation:
----------------------------------From Geany editor-----------------
from django.db import models
# Create your models here.import datetime
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_today(self): return self.pub_date.date() ==
datetime.date.today() class Choice(models.Model): poll =
models.ForeignKey(Poll) choice = models.CharField(max_length=200)
votes = models.IntegerField() def __unicode__(self):
return self.choice
----------end of script---------------
The django shell throws a wobbly and says this:
chrissmith@ubuntu:~/Dcode/mysite$ python manage.py shellTraceback
(most recent call last): File "manage.py", line 14, in <module>
execute_manager(settings) File "/usr/local/lib/python2.7/dist-
packages/django/core/management/__init__.py", line 438, in
execute_manager utility.execute() File "/usr/local/lib/python2.7/
dist-packages/django/core/management/__init__.py", line 379, in
execute self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/
base.py", line 191, in run_from_argv self.execute(*args,
**options.__dict__) File "/usr/local/lib/python2.7/dist-packages/
django/core/management/base.py", line 220, in execute output =
self.handle(*args, **options) File "/usr/local/lib/python2.7/dist-
packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options) File "/usr/local/lib/python2.7/
dist-packages/django/core/management/commands/shell.py", line 47, in
handle_noargs loaded_models = get_models() File "/usr/local/lib/
python2.7/dist-packages/django/db/models/loading.py", line 167, in
get_models self._populate() File "/usr/local/lib/python2.7/dist-
packages/django/db/models/loading.py", line 61, in _populate
self.load_app(app_name, True) File "/usr/local/lib/python2.7/dist-
packages/django/db/models/loading.py", line 78, in load_app models
= import_module('.models', app_name) File "/usr/local/lib/python2.7/
dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name) File "/home/chrissmith/Dcode/mysite/polls/
models.py", line 11 def was_published_today(self):
^If I write this however,
which i don't think is the right
indentation:----------------------------------- from Geany---------
from django.db import models
# Create your models here.import datetime
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_today(self):
return self.pub_date.date() == datetime.date.today() class
Choice(models.Model): poll = models.ForeignKey(Poll) choice =
models.CharField(max_length=200) votes = models.IntegerField()
def __unicode__(self):
return self.choice
-----end scrtpt-------
It appears to work:
chrissmith@ubuntu:~/Dcode/mysite$ python manage.py shellPython 2.7.2+
(default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2Type "help",
"copyright", "credits" or "license" for more information.
(InteractiveConsole)>>> from polls.models import Poll, Choice>>>
Poll.objects.all()[<Poll: what's up?>, <Poll: What's up?>, <Poll:
what's up now?>]>>> p = Poll.objects.get(pk=1)
Until here!
>>> p.was_published_today()Traceback (most recent call last): File "<console>", line 1, in <module>AttributeError: 'Poll' object has no attribute 'was_published_today'>>>
I have tried variations in indentation - I can get the unicode method
to work, but I cannot get the custom method in the first tutorial to
work (page 19 of 1.3.1 documentation.
I have hit a brick-wall and I do not know what I am doing wrong!
does anyone have any other suggestions?
When I try to do what i believe is the correct indentation
Chris.
On Jan 10, 7:31 am, yati sagade <yati.sag...@gmail.com> wrote:
> just like __unicode__, was_published_today is also a method/function - so
> do this:
>
> def __unicode__(self):
> return self.question
>
> def was_published_today(self):
> return self.pub_date.date() == datetime.date.today()
>
>
>
>
>
>
>
> On Tue, Jan 10, 2012 at 2:11 AM, Nasir Rasul <nasir.ra...@gmail.com> wrote:
> > check your indentation.
> > def __unicode__(self):
> > return self.question
> > def was_published_today(self):
> > return self.pub_date.date() == datetime.date.today()
> > published_today should be at the same level as __unicode__.
>
> > - Nasir
>
> > On Mon, Jan 9, 2012 at 11:02 AM, Krondaj <c.d.smi...@gmail.com> wrote:
>
> >> On page 19 of the Django documentation release 1.3.1 It tells you to
> >> add
>
> >> import datetime
> >> #...
> >> class Poll(models.Model):
> >> def was_published_today(self):
> >> return self.pub_date.date() == datetime.date.today()
>
> >> I have added this to the models.py code as including the other
> >> __unicode__ additions as:
> >> -----------------------------------------------------------------
> >> start
>
> >> code----------------------------------------------------------------------- -
> >> from django.db import models
>
> >> # Create your models here.
> >> import datetime
>
> >> 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_today(self):
> >> return self.pub_date.date() ==
> >> datetime.date.today()
>
> >> class Choice(models.Model):
> >> poll = models.ForeignKey(Poll)
> >> choice = models.CharField(max_length=200)
> >> votes = models.IntegerField()
> >> def __unicode__(self):
> >> return self.choice
> >> -----------------------------------------------------------------
> >> start
>
> >> code----------------------------------------------------------------------- -
> >> when I type in the following python commands:
>
> >> >>> Poll.objects.get(id=3)
> >> Traceback (most recent call last):
> >> File "<console>", line 1, in <module>
> >> File "/usr/local/lib/python2.7/dist-packages/django/db/models/
> >> manager.py", line 132, in get
> >> return self.get_query_set().get(*args, **kwargs)
> >> File "/usr/local/lib/python2.7/dist-packages/django/db/models/
> >> query.py", line 349, in get
> >> % self.model._meta.object_name)
> >> DoesNotExist: Poll matching query does not exist.
> >> >>> Poll.objects.get(pk=1)
> >> <Poll: what's up?>
>
> >> It all works fine until:
>
> >> >>> p = Poll.objects.get(pk=1)
> >> >>> p.was_published_today()
> >> Traceback (most recent call last):
> >> File "<console>", line 1, in <module>
> >> AttributeError: 'Poll' object has no attribute 'was_published_today'
>
> >> I know I'm probably being a thicko (i haven't programmed anything
> >> except a C-64 years ago as a kid), so i'm a n00b!
>
> >> What am i doing wrong??
>
> >> Thanks
>
> >> K
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Django users" group.
> >> To post to this group, send email to django-users@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> django-users+unsubscribe@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/django-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django-users@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 comments:
Post a Comment