Code

Opened 20 months ago

Closed 20 months ago

Last modified 20 months 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 20 months ago.
models.py

Download all attachments as: .zip

Change History (6)

comment:1 Changed 20 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

I think you must have made a mistake somewhere.

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

comment:2 Changed 20 months ago by anonymous

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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 Changed 20 months ago by timo

  • Resolution set to worksforme
  • Status changed from reopened to closed

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.

Changed 20 months ago by anonymous

models.py

comment:4 Changed 20 months ago by anonymous

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 Changed 20 months ago by Keryn Knight <django@…>

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.