from books.models import Publisher
插入:
p1 = Publisher(name = ‘Apress‘, address = ‘2855 Telegraph Avenue‘, city = ‘Berkeley‘, state_province = ‘CA‘, country = ‘U.S.A‘, website = ‘http://www.apress.com/‘)
p1.save()
一步插入(create):
p1 = Publisher.objects.create(name = ‘Apress‘, address = ‘2855 Telegraph Avenue‘, city = ‘Berkeley‘, state_province = ‘CA‘, country = ‘U.S.A‘, website = ‘http://www.apress.com/‘)
查找:
publish_list = Publish.objects.all()
publish_list
__unicode__()方法返回多个列值则稍微复杂一些,它将first_name和last_name字段值以空格连接后再返回,如下:
def __unicode__(slef):
return u‘%s %s‘%(self.first_name, self.last_name)
//对__unicode__()的唯一要求就是它要返回一个unicode对象 如果`` __unicode__()`` 方法未返回一个Unicode对象,而返回比如说一个整型数字,那么Python将抛出一个`` TypeError`` 错误,并提示:”coercing to Unicode: need string or buffer, int found” 。
Publisher.objects.filter(name = ‘Apress‘) //filter 相当于sql语句where
Publisher.objects.filter(country = ‘U.S.A‘, state_province = ‘CA‘) //相当于sql语句 where ... and ...
Publisher.objects.filter(name__contains = ‘press‘) //双下划线会将contains翻译成sql语句里的 like
select id,name,address,city,state_province,conntry,website from books_publisher where name like ‘%press%‘;
其他的一些查找类型:icontains(大小写无关的like);startswith和endswith;还有range(SQL中between查询)
获取单个对象(即一个结果集QuerySet,如果查询结果是多个对象,会导致抛出异常;如果查询没有返回结果也会抛出异常)
Publisher.objects.get(name = ‘Apress‘)
这个 DoesNotExist 异常 是 Publisher 这个 model 类的一个属性,即 Publisher.DoesNotExist。在你的应用中,你可以捕获并处理这个异常,像这样:
try: p = Publisher.objects.get(name=‘Apress‘) except Publisher.DoesNotExist: print "Apress isn‘t in the database yet." else: print "Apress is in the database."
Publisher.objects.order_by("name")
Publisher.objects.order_by("state_province", "address")
Publisher.objects.order_by("-name") //逆向排序
Django让你可以指定模型的缺省排序方式:
class Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30) country = models.CharField(max_length=50) website = models.URLField() def __unicode__(self): return self.name class Meta: ordering = [‘name‘] //你可以在任意一个 模型 类中使用 Meta 类,来设置一些与特定模型相关的选项。 在 附录B 中有 Meta 中所有可选项的完整参考,现在,我们关注 ordering 这个选项就够了。Publisher.objects.filter(countru = ‘U.S.A‘).order_by("-name") //连锁查询Publisher.objects.order_by(‘name‘)[0] //相当于sql: select id,name,address,city,state_province,country,website from book_publisher order by name limit 1;
Publisher.objects.order_by(‘name‘)[0,2] //相当于sql: select id,name,address,city,state_province,country,website from book_publisher order by name offset 0 limit 2;
Publiser.objects.order_by("-name")[0] //查找“最后一个” 更新:
p1.name = ‘Apress Publishing‘
p1.save()
p1.id //假设结果为52
以上两句相当于:update books_publisher set name=‘Apress Publishing‘,address = ‘2855 Telegraph Avenue‘, city = ‘Berkeley‘, state_province = ‘CA‘, country = ‘U.S.A‘, website = ‘http://www.apress.com/‘ where p1.id=52
改进后:
Publisher.objects.filter(id = 52).update(name = ‘Apress Publishing‘)
相当于:update books_publisher set name=‘Apress Publishing‘ where id=52;
Publisher.objects.all().update(country=‘USA‘) //把所有country值都变为USA
删除:
p = Publisher.objects.get(name = "Apress")
p.delete()
Publisher.objects.filter(name = ‘Apress‘).delete()