Hi, model Choice is related to model Poll by ForeignKey. All Choice objects related to specific Poll object can by returned by:
my_poll.choice_set.all() And that is happening in the template in for loop: {% for choice in poll.choice_set.all %} ... More detailed info is here: https://docs.djangoproject.com/en/1.4/topics/db/queries/#backwards-related-objects Honza On Fri, Oct 12, 2012 at 12:06 PM, Rick Chong <ulian...@gmail.com> wrote: > Hi, I have just started learning programming and I am following the creating > a poll app tutorial at: > https://docs.djangoproject.com/en/1.4/intro/tutorial03/ > > I hope that someone has attempted this tutorial and is able to help me on > some very beginner questions: > > In this app, we defined 2 classes in the model - Poll & Choice: >>> >>> import datetime >>> >>> from django.utils import timezone >>> >>> from django.db import models >>> >>> >>> >>> # Create your models here. >>> >>> 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) >>> >>> was_published_recently.admin_order_field = 'pub_date' >>> >>> was_published_recently.boolean = True >>> >>> was_published_recently.short_description = 'Published recently?' >>> >>> >>> class Choice(models.Model): >>> >>> poll = models.ForeignKey(Poll) >>> >>> choice = models.CharField(max_length=200) >>> >>> votes = models.IntegerField() >>> >>> def __unicode__(self): >>> >>> return self.choice > > > > Then in views.py... I have the following code: >>> >>> from django.shortcuts import render_to_response, get_object_or_404 >>> >>> from polls.models import Poll >>> >>> >>> def detail(request, poll_id): >>> >>> p = get_object_or_404(Poll, pk=poll_id) >>> >>> return render_to_response('polls/detail.html', {'poll': p}) >> >> > > > Then in my template, detail.html. I have the following code: >>> >>> <h1>{{ poll.question }}</h1> >>> >>> <ul> >>> >>> {% for choice in poll.choice_set.all %} >>> >>> <li>{{ choice.choice }}</li> >>> >>> {% endfor %} >>> >>> </ul> > > > > > The scripts run My question: > In views.py, I only import Poll class from model... why am I able to display > choice, which is a variable under Choice class? > > > Thank you very much. > > > > > Rick > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/2vqJuWiYVuIJ. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.