打开django shell
python manage.py shell
输入命令
>>> from myapp import *
>>> MySite.objects.all()
插入一条命令
m=Mysite(title='django',num=2)
m.save()
然后再查询
>>> m=Mysite(title='django',num=2)
>>> m.save()
>>> Mysite.objects.all()
[<Mysite: Mysite object>]
>>> m=Mysite.objects.all()
>>> m[0]
<Mysite: Mysite object>
>>> m[0].title
过滤
>>> m=Mysite.objects.get(num=2)
>>> m.title
排序
>>> m=Mysite(title='lala',num='3')
>>> m.save()
>>> m=Mysite(title='dongshen',num='1')
>>> m.save()
>>> m=Mysite(title="niusen",num='5')
>>> m.save()
>>> m=Mysite.objects.all().order_by("num")
>>> m[0].num
//倒序排列
>>> m=Mysite.objects.all().order_by("-num")
>>> m[0].num
也可以修改models,这样每次都可以按照num排序
from django.db import models
# Create your models here.
class Mysite(models.Model):
title=models.CharField(max_length=100)
url=models.URLField()
author=models.CharField(max_length=100)
num=models.IntegerField(max_length=10)
class Meta:
ordering=['num']
更新
>>> x=m[0]
>>> x
<Mysite: Mysite object>
>>> x.title
u'niusen'
>>> x.title="loongshen"
>>> x.save()
>>> x.title
'loongshen'
删除
>>> x.delete()
>>> Mysite.objects.get(num=0)
限制只取前几条数据
Mysite.objects.all()[0:1]