| 171 | |
| 172 | == QuerySets aren't Lists == |
| 173 | |
| 174 | ==== Problem ==== |
| 175 | |
| 176 | You have been playing with the database API and noticed that a returned query set looks a lot like a list: |
| 177 | |
| 178 | {{{ |
| 179 | #!python |
| 180 | >>> from mysite.polls.models import Poll,Choice |
| 181 | >>> Poll.objects.all() |
| 182 | [<Poll: What is up?>, <Poll: What is your favourite colour?>] ## looks a lot like a list to me |
| 183 | }}} |
| 184 | |
| 185 | But, it doesn't behave like a list in some cases. |
| 186 | |
| 187 | ==== Solution ==== |
| 188 | |
| 189 | Here are a couple of cases where the behaviour is not list-like and their solution. |
| 190 | |
| 191 | In Python this is how we can test for an empty list: |
| 192 | |
| 193 | {{{ |
| 194 | #!python |
| 195 | >>> b=[] |
| 196 | >>> b==[] |
| 197 | True |
| 198 | }}} |
| 199 | |
| 200 | This doesn't work with a QuerySet. You might try the following but it will fail: |
| 201 | |
| 202 | {{{ |
| 203 | #!python |
| 204 | >>> from mysite.polls.models import Poll,Choice |
| 205 | >>> p = Poll.objects.filter(question__startswith='ZZZZZZZZZZZZ') |
| 206 | >>> p |
| 207 | [] |
| 208 | >>> p==[] |
| 209 | False |
| 210 | }}} |
| 211 | |
| 212 | The way to do it is test for the length of p: |
| 213 | |
| 214 | {{{ |
| 215 | #!python |
| 216 | >>> from mysite.polls.models import Poll,Choice |
| 217 | >>> p = Poll.objects.filter(question__startswith='ZZZZZZZZZZZZ') |
| 218 | >>> p |
| 219 | [] |
| 220 | >>> len(p) == 0 |
| 221 | True |
| 222 | }}} |
| 223 | |
| 224 | Another case is when you want to retrieve the last member of a QuerySet: |
| 225 | |
| 226 | {{{ |
| 227 | #!python |
| 228 | >>> from mysite.polls.models import Poll,Choice |
| 229 | >>> p = Poll.objects.all() |
| 230 | >>> p |
| 231 | [<Poll: What is up?>, <Poll: What is your favourite colour?>] |
| 232 | >>> p[-1] |
| 233 | Traceback (most recent call last): |
| 234 | File "<console>", line 1, in ? |
| 235 | File "c:\python24\lib\site-packages\django-0.95-py2.4.egg\django\db\models\query.py", line 98, in |
| 236 | __getitem__ |
| 237 | assert (not isinstance(k, slice) and (k >= 0)) \ |
| 238 | AssertionError: Negative indexing is not supported. |
| 239 | }}} |
| 240 | |
| 241 | The way I get the last member is: |
| 242 | |
| 243 | {{{ |
| 244 | #!python |
| 245 | >>> p[len(p)-1] |
| 246 | <Poll: What is your favourite colour?> |
| 247 | }}} |