django(5):数据库模型models

创建学生类

 

vim app0904/models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.



class Student(models.Model):
  id = models.IntegerField(primary_key=True)
  name = models.CharField(max_length=50)
  age=models.IntegerField()
  sex=models.IntegerField()
  class Meta:
    db_table='student'

 同步数据库

 

 

[root@localhost project0904]# python manage.py makemigrations 
Migrations for 'app0904':
  app0904/migrations/0001_initial.py:
    - Create model Student
[root@localhost project0904]# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, app0904, auth, contenttypes, sessions
Running migrations:
  Applying app0904.0001_initial... OK

 插入几个测试的Student

 

 

 

[root@localhost project0904]# python manage.py shell
Python 2.7.5 (default, Jun 17 2014, 18:11:42) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 
>>> 
>>> 
>>> from app0904.models import Student
>>> Student.objects.create(id=1,name='zhangsan',age=11,sex=0)  
<Student: Student object>
>>> Student.objects.all()
<QuerySet [<Student: Student object>]>
>>> Student.objects.all().get(id=1)
<Student: Student object>
>>> Student.objects.all()[0].id
1
>>> Student.objects.all()[0].name
u'zhangsan'
>>> 
>>> 
>>> Student.objects.create(id=2,name='lisi',age=12,sex=0)    
<Student: Student object>

 创建测试模版

 

 

from django.shortcuts import render
from app0904.models import Student



def student_list(request):
  #studentList=Student.objects.all()#所有学生
  #studentList=Student.objects.order_by('-age','id')#年龄倒序 id排序
  studentList=Student.objects.filter(age__gte=11)#年龄大于11
  return render(request,'student_list.html', {'studentList':studentList})

 修改URLs.py

 

[root@localhost project0904]# vim project0904/urls.py

from django.conf.urls import url
from django.contrib import admin
from app0904 import views
urlpatterns=[
url(r'^$',views.main),
url(r'^admin/',admin.site.urls),
url(r'^studentlist/$',views.student_list),




url(r'^hello/$',views.hello),
url(r'^hellop1/(.+)/$',views.helloParam),
url(r'^hello/(.+)/(.+)/$',views.helloParams),
url(r'^hellopp/',views.helloPar)]

 

创建html文件

[root@localhost templates]# pwd
/root/project0904/app0904/templates
[root@localhost templates]# vim student_list.html


{% extends 'base.html' %}

{% block title%}studentlist{%endblock%}

{% block content%}

{%for student in studentList%}

<li>{{student.id}},{{student.name}},{{student.age}},{{student.sex}}</li>
{%empty%}
sorry
{% endfor %}

{% endblock %}

 

 

django ORM model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct

 

1.多表连接查询:感觉django太NX了。
  class A(models.Model):
    name = models.CharField(u'名称')
  class B(models.Model):
    aa = models.ForeignKey(A)
B.objects.filter(aa__name__contains='searchtitle')

1.5 反向查询,补上记录1.5,感觉django太太太NX了。
  class A(models.Model):
    name = models.CharField(u'名称')
  class B(models.Model):
    aa = models.ForeignKey(A,related_name="FAN")
    bb = models.CharField(u'名称')
  查A: A.objects.filter(FAN__bb='XXXX'),都知道related_name的作用,A.FAN.all()是一组以A为外键的B实例,可前面这样的用法是查询出所有(B.aa=A且B.bb=XXXX)的A实例,然后还可以通过__各种关系查找,很好用!!!

2.条件选取querySet的时候,filter表示=,exclude表示!=。
querySet.distinct() 去重复
__exact 精确等于 like 'aaa'
 __iexact 精确等于 忽略大小写 ilike 'aaa'
 __contains 包含 like '%aaa%'
 __icontains 包含 忽略大小写 ilike '%aaa%',但是对于sqlite来说,contains的作用效果等同于icontains。
__gt 大于
__gte 大于等于
__lt 小于
__lte 小于等于
__in 存在于一个list范围内
__startswith 以...开头
__istartswith 以...开头 忽略大小写
__endswith 以...结尾
__iendswith 以...结尾,忽略大小写
__range 在...范围内
__year 日期字段的年份
__month 日期字段的月份
__day 日期字段的日
__isnull=True/False

例子:
>> q1 = Entry.objects.filter(headline__startswith="What")
>> q2 = q1.exclude(pub_date__gte=datetime.date.today())
>> q3 = q1.filter(pub_date__gte=datetime.date.today())
>>> q = q.filter(pub_date__lte=datetime.date.today())
>>> q = q.exclude(body_text__icontains="food")

即q1.filter(pub_date__gte=datetime.date.today())表示为时间>=now,q1.exclude(pub_date__gte=datetime.date.today())表示为<=now

2013/12/12补充:
“在django models中取得一个字段的distinct值”。就是select distinct xxx from table_name ...这样的功能。使用values会生成ValuesQuerySet(形如N个dict组成的list),猜测大数据无额外性能影响,毕竟queryset系列都是使用时才查询操作的。
xxxx.objects.values("field_name").distinct()
#或者
xxxx.objects.distinct().values("field_name")
这两句生成的sql语句相同

关于缓存:
queryset是有缓存的,a = A.objects.all(),print [i for i in a].第一次执行打印会查询数据库,然后结果会被保存在queryset内置的cache中,再执行print的时候就会取自缓存。
很多时候会遇到仅需判断queryset是否为空的情况,可以1. if queryset:pass 2.if queryset.count>0:pass 3.if queryset.exists():pass. 三种方式性能依次提升。
当queryset非常巨大时,cache会成为问题。此时可以queryset.iterator(),迭代器的用处就不多说了,根据具体需求情况使用。

 

 

添加一行数据1
add = myclass(aa='wahaha',bb='hahawa'  )
add.save() #不save无法保存到数据库
add.id #获取增加的这条数据的ID
添加一行数据2
myclass.objects.create(aa='wahaha',bb='hahawa')  #  同上1方法一样无需保存save
 
 
删除表中全部数据
myclass.objects.all().delete()
删除一条aa等于'test'的数据
myclass.objects.get(aa='test').delete()
删除多条数据
myclass.objects.filter(aa='123').delete() #过滤出aa字段等于123的都删除
 
查出库中所有条数的数据
myclass.objects.all()
查询带字段名的所有条数数据
myclass.objects.all().values()
查询单挑数据
myclass.objects.get(aa='123') #查询aa字段中是123的这条数据,如果是多条和没有的时候会报错,尽量结合try:except使用 
查询匹配条件的多条数据
myclass.objects.filter(aa='123') #查询aa字段值为123的所有数据条数,括号的匹配条件可多个,以逗号分隔
模糊查询
myclass.objects  .filter(aa__contains="1") #查询aa字段中值包含'1'的数据,例如aa字段值为 123 154 这两条都能匹配
根据字段内容排序后展示数据
myclass.objects.order_by('aa')  #根据aa字段的内容进行数据排序,会根据字母和数字排序
根据字段内容逆向排序后展示数据,加一个负号
 
myclass.objects  .order_by('-aa'  )  #根据aa字段的内容进行逆向数据排序,会根据字母和数字排序
连锁查询,先过滤,过滤后进行逆向排序
 

myclass.objects.filter(aa='123'.order_by("‐aa")

限制数据条数,相当于mysql limit
myclass.objects.filter(aa='123')[0]  #[0]显示第一条 [0:2]会显示前两条
myclass.objects.filter(aa='123').order_by("‐aa")[0] #切片不支持负数,这样就数据序列倒过来的第一条,也就是最后一条数据 

 
更新数据
a = userinfo.objects.get(cellPhone='13133333333') #查询一条你要更新的数据
a.cellPhone='3111111111111'   #赋值给你要更新的字段
a.save() #保存
更新多个字段或一个字段
myclass.objects  .get(aa='123').update(aa='321',bb="wahaha") #update可多条
更新所有字段

 

myclass.objects.all().update(aa='8888') #更新所有字段,更新后会显示受影响的条数
 
Student.objects.all()[:10] 切片操作,获取10个人,不支持负索引,切片可以节约内存
 
get获取满足条件的一些人用filter
 
Student.objects.filter(name="abc") # 等于Student.objects.filter(name__exact="abc") 名称严格等于 "abc" 的人
 
Student.objects.filter(name__iexact="abc") # 名称为 abc 但是不区分大小写,可以找到 ABC, Abc, aBC,这些都符合条件
 
 
 
Student.objects.filter(name__contains="abc") # 名称中包含 "abc"的人
 
Student.objects.filter(name__icontains="abc") #名称中包含 "abc",且abc不区分大小写
 
 
 
Student.objects.filter(name__regex="^abc") # 正则表达式查询
 
Student.objects.filter(name__iregex="^abc")# 正则表达式不区分大小写

 

 

猜你喜欢

转载自886.iteye.com/blog/2323790