Changes between Version 22 and Version 23 of NewbieMistakes


Ignore:
Timestamp:
Jul 7, 2006, 9:35:41 AM (18 years ago)
Author:
Paul Childs
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NewbieMistakes

    v22 v23  
    169169
    170170Make sure Apache can also write to the parent directory of the database. SQLite needs to be able to write to this directory.
     171
     172== QuerySets aren't Lists ==
     173
     174==== Problem ====
     175
     176You 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
     185But, it doesn't behave like a list in some cases.
     186
     187==== Solution ====
     188
     189Here are a couple of cases where the behaviour is not list-like and their solution.
     190
     191In Python this is how we can test for an empty list:
     192
     193{{{
     194#!python
     195>>> b=[]
     196>>> b==[]
     197True
     198}}}
     199
     200This 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==[]
     209False
     210}}}
     211
     212The 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
     221True
     222}}}
     223
     224Another 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]
     233Traceback (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)) \
     238AssertionError: Negative indexing is not supported.
     239}}}
     240
     241The way I get the last member is:
     242
     243{{{
     244#!python
     245>>> p[len(p)-1]
     246<Poll: What is your favourite colour?>
     247}}}
Back to Top