Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19311 closed Uncategorized (worksforme)

"c.poll" did not work at the tutorial 01 (last example on the page)

Reported by: vijayky@… Owned by: nobody
Component: Documentation Version: 1.4
Severity: Normal Keywords:
Cc: vijayky@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

URL: https://docs.djangoproject.com/en/1.4/intro/tutorial01/


Documentation:

c = p.choice_set.create(choice='Just hacking again', votes=0)

# Choice objects have API access to their related Poll objects.

c.poll

<Poll: What's up?>


Interactive Output:

c = p.choice_set.create(choice='Just hacking again', votes=0)
c.poll

Traceback (most recent call last):

File "<console>", line 1, in <module>

AttributeError: 'Choice' object has no attribute 'poll'


What Works

c.question

<Poll: What's up?>

Attachments (1)

models.py (826 bytes ) - added by anonymous 12 years ago.
models.py

Download all attachments as: .zip

Change History (6)

comment:1 by Tim Graham, 12 years ago

Resolution: worksforme
Status: newclosed

I think you must have made a mistake somewhere.

https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels

comment:2 by anonymous, 12 years ago

Resolution: worksforme
Status: closedreopened

Here is the whole list what I did. I think it works for you because I am working on 1.4.2 and document is created for 1.4.
The Issue tracker does not have option to select 1.4.2 in version list.

django.get_version()

'1.4.2'
.
.
.
c:\Temp\django_projects\mysite
$ python manage.py shell
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

from polls.models import Poll, Choice
Poll.objects.all()

[<Poll: What's up?>]

Poll.objects.filter(id=1)

[<Poll: What's up?>]

Poll.objects.filter(questionstartswith="What")

[<Poll: What's up?>]

Poll.objects.get(pub_dateyear=2012)

<Poll: What's up?>

Poll.objects.get(id=2)

Traceback (most recent call last):

File "<console>", line 1, in <module>
File "C:\Python27\ArcGIS10.1\lib\site-packages\django\db\models\manager.py", line 131, in get

return self.get_query_set().get(*args, kwargs)

File "C:\Python27\ArcGIS10.1\lib\site-packages\django\db\models\query.py", line 366, in get

% self.model._meta.object_name)

DoesNotExist: Poll matching query does not exist.

Poll.objects.get(pk=1)

<Poll: What's up?>

p = Poll.objects.get(pk=1)
p.was_published_recently()

True

p

<Poll: What's up?>

p.choice_set.all()

[]

p.choice_set.create(choice='Not much', votes=0)

<Choice: Not much>

p.choice_set.create(choice='The sky', votes= 0)

<Choice: The sky>

c = p.choice_set.create(choice='Just hacking again', votes=0)
c.poll

Traceback (most recent call last):

File "<console>", line 1, in <module>

AttributeError: 'Choice' object has no attribute 'poll'

c.poll()

Traceback (most recent call last):

File "<console>", line 1, in <module>

AttributeError: 'Choice' object has no attribute 'poll'

comment:3 by Tim Graham, 12 years ago

Resolution: worksforme
Status: reopenedclosed

I think you have a mistake in your polls/models.py file. Does it look like the tutorial instructs?

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

Please use the support channels in the link above if you have problems.

by anonymous, 12 years ago

Attachment: models.py added

models.py

comment:4 by anonymous, 12 years ago

Here is my rerun of the interactive shell commands and model is enclosed. Since I have proceeded to tutorial 3 but it is not different from your model. See enclosed file (model.py: https://code.djangoproject.com/attachment/ticket/19311/models.py) for the model reference.

c:\Temp\django_projects\mysite
$ python manage.py shell
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

from polls.models import Poll, Choice
Poll.objects.all()

[<Poll: What's new?>]

p = Poll.objects.get(pk=1)
c = p.choice_set.create(choice='Just hacking again', votes=0)
c.poll

Traceback (most recent call last):

File "<console>", line 1, in <module>

AttributeError: 'Choice' object has no attribute 'poll'

comment:5 by Keryn Knight <django@…>, 12 years ago

Your Choice model uses question rather than poll as a foreign key to Poll.

Note: See TracTickets for help on using tickets.
Back to Top