标签:update sele family mil not int tle space blog
简介
# -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom django.db import modelsclass Blog(models.Model):name = models.CharField(max_length=100)tagline = models.TextField()def __unicode__(self):return self.name
3. 增
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.(InteractiveConsole)>>> from web.models import *>>> b = Blog(name=‘Beates Blog‘, tagline=‘All the latest Beatles news.‘)>>> b.save()>>>>>> b.id1L>>> b.name‘Beates Blog‘>>> b.tagline‘All the latest Beatles news.‘
>>> Blog.objects.create(name=‘wangZhen Blog‘, tagline=‘All the django blog.‘)<Blog: wangZhen Blog>
4. 查
>>> Blog.objects.get(name = ‘Beates Blog‘)<Blog: Beates Blog>
Traceback (most recent call last):File "<console>", line 1, in <module>File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 85, in manager_methodreturn getattr(self.get_queryset(), name)(*args, **kwargs)File "C:\Python27\lib\site-packages\django\db\models\query.py", line 380, in getself.model._meta.object_nameDoesNotExist: Blog matching query does not exist.
icontains(大小写无关的LIKE);
<QuerySet [<Blog: wangZhen Blog>]>>>> Blog.objects.filter(tagline__contains = ‘latest‘)<QuerySet [<Blog: Beates Blog>]>>>> Blog.objects.filter(name__icontains = ‘blog‘)<QuerySet [<Blog: Beates Blog>, <Blog: wangZhen Blog>]>
<QuerySet [<Blog: Beates Blog>, <Blog: wangZhen Blog>]>>>> Blog.objects.all()[1]<Blog: wangZhen Blog>
>>> a.name = ‘test0‘>>> a.save()>>> Blog.objects.get(id = 1).name错误:u‘test0‘
>>> Blog.objects.get(id = 1).save()>>> Blog.objects.get(id = 1).nameu‘test0‘
2) 更新多条数据的多个字段或一个字段
>>> Blog.objects.filter(id__in=[1,2]).update(name = ‘test‘)1L
>>> Blog.objects.all().update(name = ‘test‘)
cursor.execute(‘‘‘SELECT DISTINCT first_name ROM people_person WHERE last_name = %s""", [‘Lennon‘])row = cursor.fetchone()
Django 07. django框架模型之增删改查基本操作
标签:update sele family mil not int tle space blog
原文地址:http://www.cnblogs.com/PythonHomePage/p/7634396.html