Monday, July 2, 2012

Re: Unable to obtain c.poll

Thanks DR. Now I understand the difference between filter and get.

On Mon, Jul 2, 2012 at 1:02 PM, Daniel Roseman <daniel@roseman.org.uk> wrote:
On Monday, 2 July 2012 20:47:40 UTC+1, Sam007 wrote:
Hi Djangoers,

I am almost completing my first tutorial but got stuck at the last point. So I am assigning Choice object to 'c',

c = p.choice_set.filter(choice_text__startswith='Just')

And I am getting this output when I check 'c'

[<Choice: Just Hacking again>]

And I am also getting the right output for 'p'

<Poll: WASSUP!!!!>

But when I am executing,

     c.poll

This is my output,

Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'poll'

Not sure where I am going wrong?

--
Thanks & Regards
Smaran Harihar


You're not assigning a Choice object to `c`. You're assigning a queryset (basically, a list) of one Choice objects to c - hence the square brackets when you print it. As the documentation explains, `filter` *always* returns a queryset, even if only one object matches.

You can do c[0] to get the Choice object, or use `get()` instead of `filter()` (although note that will raise an exception if zero or > 1 objects match).
--
DR.

--
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/-/p-sNQq_41PUJ.
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.



--
Thanks & Regards
Smaran Harihar

--
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.

No comments:

Post a Comment