Aug 022009

Use
Python’s standard list-slicing syntax.
———–
e.g.,
>>>Card.objects.order_by(”name”)[0]
First one only
>>>Card.objects.order_by(”name”)[0:2]
The first two objects will be returned

Posted by weboom at 5:51 am Tagged with: , , ,
Aug 022009

e.g.,
>>>Card.objects.filter(city=”San Diego”).order_by(”-name”)

Posted by weboom at 5:45 am Tagged with: ,
Aug 022009

In model definition,
class Meta:
ordering = ['name']

Posted by weboom at 5:41 am Tagged with: , , ,
Aug 022009

Use
objects.order_by()
—————-
e.g.,
>>>Card.objects.order_by(”name”)
for reverse order
>>>Card.objects.order_by(”-name”)

Posted by weboom at 5:40 am Tagged with: , , ,
Aug 022009

filter()
retrieves a QuerySet which is like a list.
To retrieve a single object, use
get()
——————-
e.g.,
>>>Card.objects.get(name=’Hot Pot’)
<Card: Hot Pot>

Posted by weboom at 5:37 am Tagged with: , ,
Aug 022009

Use
objects.filter
———
e.g.,
>>>Card.objects.filter(name=’Hot Pot’)
>>>Card.objects.filter(name=’Hot Pot’, city=’San Diego’)
>>>Card.objects.filter(name_contains=’Hot’)
[<Card: Hot Pot>]
Note that here filter() returned a
QuerySet
which is like a list.

Posted by weboom at 5:35 am Tagged with: , , ,
Aug 022009

__unicode__
is for displaying objects.
———–
For example,
>>>p = Card.objects.create(name=’Hot Pot’, city=’San Diego’)
>>>card_list = Card.objects.all()
>>>card_list
[<Card: Card object>]
without __unicode__, you will see just “Card object”
if you define __unicode__
def __unicode__(self):
return self.name
then, you will see
[<Card: Hot Pot>]

Posted by weboom at 5:22 am Tagged with: , ,
Aug 022009

Use the attribute
objects
of models.
Here “objects” is a manager which takes care of all data lookup operation!
To fetch all objects, use
objects.all()
This will return the list of objects.
———
>>>card_list=Card.objects.all()

Posted by weboom at 5:08 am Tagged with: , , ,
Aug 022009

$ python manage.py syncdb
is for sync of your models to your database.
It looks all of the models in each app in INSTALLED_APPS setting,
checks the database to see whether the appropriate table exist yet or not.
If not, create the tables.
Note that syncdb does not sync changes in models or deletions of models. That is, if you [...]

Posted by weboom at 4:49 am Tagged with: , , ,
Aug 022009

$ python manage.py sqlall yourappname
prints out all the database tables associated with yourapp.
Note that it is not actually create the tables.
To commit, use
$ python manage.py syncdb

Posted by weboom at 4:46 am Tagged with: , ,
Switch to our mobile site
Page 2 of 5«12345»Top Footer