Unable to obtain c.poll

22 views
Skip to first unread message

Smaran Harihar

unread,
Jul 2, 2012, 3:47:40 PM7/2/12
to django...@googlegroups.com
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

Daniel Roseman

unread,
Jul 2, 2012, 4:02:04 PM7/2/12
to django...@googlegroups.com
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.

Smaran Harihar

unread,
Jul 2, 2012, 5:34:42 PM7/2/12
to django...@googlegroups.com
Thanks DR. Now I understand the difference between filter and get.

--
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...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Smaran Harihar

unread,
Jul 2, 2012, 5:37:26 PM7/2/12
to django...@googlegroups.com
Also when we are taking,


 c = p.choice_set.filter(choice_text__startswith='Just hacking')
>>> c.delete()

Are we deleting the object or just that filter queryset choice? 

On Mon, Jul 2, 2012 at 1:02 PM, Daniel Roseman <dan...@roseman.org.uk> wrote:

--
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...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Melvyn Sopacua

unread,
Jul 4, 2012, 8:34:02 AM7/4/12
to django...@googlegroups.com
On 2-7-2012 23:37, Smaran Harihar wrote:
> Also when we are taking,
>
> c = p.choice_set.filter(choice_text__startswith='Just hacking')>>> c.delete()
>
>
> Are we deleting the object or just that filter queryset choice?

The list returned by the filter.
c = p.choice_set.get(..)
c.delete() # calls the delete method of the choice object

--
Melvyn Sopacua


Reply all
Reply to author
Forward
0 new messages