5、ORM
a. 创建类和字段
class User(models.Model):
age = models.IntergerFiled()
name = models.CharField(max_length=10)#字符长度
Python manage.py makemigrations
python manage.py migrate
# settings.py 注册APP
b. 操作
增
直接增加:models.User.objects.create(name = ‘xx’)
通过字典增加:dic = {'name': 'xx', 'age': 19}
models.User.objects.create(**dic)
先增加后保存:obj = models.User(name='xx',age=18)
obj.save()
删
models.User.objects.filter(id=1).delete()
改
models.User.objects.filter(id__gt=1).update(name='xx',age=84)
通过字典改:dic = {'name': 'xx', 'age': 19}
models.User.objects.filter(id__gt=1).update(**dic)
查
models.User.objects.filter(id=1,name='xx')
models.User.objects.filter(id__gt=1,name='xx')
models.User.objects.filter(id__lt=1)
models.User.objects.filter(id__gte=1)
models.User.objects.filter(id__lte=1)
dic = {'name': 'xx', 'age__gt': 19}
models.User.objects.filter(**dic)
v1 = models.Business.objects.all()
# QuerySet ,内部元素都是对象
# QuerySet ,内部元素都是字典
v2 = models.Business.objects.all().values('id','caption')
# QuerySet ,内部元素都是元组
v3 = models.Business.objects.all().values_list('id','caption')
# 获取到的一个对象,如果不存在就报错
models.Business.objects.get(id=1)
或者None = models.Business.objects.filter(id=1).first()
外键:
v = models.Host.objects.filter(nid__gt=0)
v[0].b.caption ----> 通过.进行跨表
外键:
class UserType(models.Model):
caption = models.CharField(max_length=32)
class User(models.Model):
age = models.IntergerFiled()
name = models.CharField(max_length=10)#字符长度
Python manage.py makemigrations
python manage.py migrate
# settings.py 注册APP
b. 操作
增
直接增加:models.User.objects.create(name = ‘xx’)
通过字典增加:dic = {'name': 'xx', 'age': 19}
models.User.objects.create(**dic)
先增加后保存:obj = models.User(name='xx',age=18)
obj.save()
删
models.User.objects.filter(id=1).delete()
改
models.User.objects.filter(id__gt=1).update(name='xx',age=84)
通过字典改:dic = {'name': 'xx', 'age': 19}
models.User.objects.filter(id__gt=1).update(**dic)
查
models.User.objects.filter(id=1,name='xx')
models.User.objects.filter(id__gt=1,name='xx')
models.User.objects.filter(id__lt=1)
models.User.objects.filter(id__gte=1)
models.User.objects.filter(id__lte=1)
dic = {'name': 'xx', 'age__gt': 19}
models.User.objects.filter(**dic)
v1 = models.Business.objects.all()
# QuerySet ,内部元素都是对象
# QuerySet ,内部元素都是字典
v2 = models.Business.objects.all().values('id','caption')
# QuerySet ,内部元素都是元组
v3 = models.Business.objects.all().values_list('id','caption')
# 获取到的一个对象,如果不存在就报错
models.Business.objects.get(id=1)
或者None = models.Business.objects.filter(id=1).first()
外键:
v = models.Host.objects.filter(nid__gt=0)
v[0].b.caption ----> 通过.进行跨表
外键:
class UserType(models.Model):
caption = models.CharField(max_length=32)
class User(models.Model):
age = models.IntergerFiled()
name = models.CharField(max_length=10)#字符长度
# user_type_id = models.IntergerFiled() # 约束,
user_type = models.ForeignKey("UserType",to_field='id') # 约束,
Ajax
$.ajax({
url: '/host',
type: "POST",
data: {'k1': 123,'k2': "root"},
success: function(data){
// data是服务器端返回的字符串
var obj = JSON.parse(data);
}
})
建议:永远让服务器端返回一个字典
return HttpResponse(json.dumps(字典))
多对多:
创建多对多:
方式一:自定义关系表
class Host(models.Model):
nid = models.AutoField(primary_key=True)
hostname = models.CharField(max_length=32,db_index=True)
ip = models.GenericIPAddressField(protocol="ipv4",db_index=True)
port = models.IntegerField()
b = models.ForeignKey(to="Business", to_field='id')
# 10
class Application(models.Model):
name = models.CharField(max_length=32)
# 2
class HostToApp(models.Model):
hobj = models.ForeignKey(to='Host',to_field='nid')
aobj = models.ForeignKey(to='Application',to_field='id')
# HostToApp.objects.create(hobj_id=1,aobj_id=2)
方式二:自动创建关系表
class Host(models.Model):
nid = models.AutoField(primary_key=True)
hostname = models.CharField(max_length=32,db_index=True)
ip = models.GenericIPAddressField(protocol="ipv4",db_index=True)
port = models.IntegerField()
b = models.ForeignKey(to="Business", to_field='id')
class Application(models.Model):
name = models.CharField(max_length=32)
r = models.ManyToManyField("Host")
无法直接对第三张表进行操作
obj = Application.objects.get(id=1)
obj.name
# 第三张表操作
obj.r.add(1)
obj.r.add(2)
obj.r.add(2,3,4)
obj.r.add(*[1,2,3,4])
obj.r.remove(1)
obj.r.remove(2,4)
obj.r.remove(*[1,2,3])
obj.r.clear()
obj.r.set([3,5,7])
obj.r.all()
name = models.CharField(max_length=32)
r = models.ManyToManyField("Host")
无法直接对第三张表进行操作
obj = Application.objects.get(id=1)
obj.name
# 第三张表操作
obj.r.add(1)
obj.r.add(2)
obj.r.add(2,3,4)
obj.r.add(*[1,2,3,4])
obj.r.remove(1)
obj.r.remove(2,4)
obj.r.remove(*[1,2,3])
obj.r.clear()
obj.r.set([3,5,7])
obj.r.all()