首页 > 代码库 > django学习笔记之models
django学习笔记之models
djang models
1. 创建model
from django.db import models
class MyObject(models.Model):
attribute1 = models.CharField(max_length=50)
2. 创建记录
2.1 p = MyObject(attribute=xxxx)
p.save()
2.2 p = MyObject.objects.create(attribue=xxx)
3. 更新记录
p = MyObject.objects.all(attribute=xxx)
p.attribute = yyy
p.save()
4. 检索记录
MyObject.objects.all()
5. 过滤记录
MyObject.objects.filter(attribute=‘xxxx‘,....)
MyObject.objects.filter(attribute__contains = ‘xxxx‘)
MyObject.objects.filter(attribute__icontains=‘xcada‘)
MyObject.objects.filter(attribute__statswith=xxx)
MyObject.objects.filter(attribute__endswith=dadadad)
6. 单一检索
MyObject.objects.get(attribute=xxx)
注意:
6.1 如果返回多条记录,则抛出错误
6.2 返回为空也会报错
7. 排序
MyObject.objects.order_by(‘xxx‘, ....)
MyObject.objects.order-by(‘-xxxx‘)
8. 默认排序
class MyObjects(models.Model):
attribute = models.CharField(max_lenth=2222)
--------------------------------------------
class Meta:
ordering = [‘name‘]
9. 复合查询
待整理
本文出自 “木子博客” 博客,请务必保留此出处http://ggbond.blog.51cto.com/8886865/1538374