Python/Django(CBV/FBV/ORM操作)
CBV:url对应的类(模式)
##====================================CBV操作============================ # class geting(View):
# def dispatch(self, request, *args, **kwargs):
# print('before')
# obj = super(geting,self).dispatch(request,*args,**kwargs)
# print('after')
# return obj
#
# def get(self,request):
# print('get.get')
# return render(request,'geting.html')
#
# def post(self,request):
# print('get.post')
# print(request.POST.get('user'))
# return HttpResponse('get.post')
FBA:url对应的函数(模式)
##===================================FBV操作============================
def get(request):
print(2222)
return HttpResponse('is get') '''
get 查
post 创建
put 更新
delete 删除 常用传值方式
'''
ORM操作
##=====================================ORM操作=========================================
def gets(request):
pass
# for row in range(300):
# name='root'+str(row)
# models.UserInfo.objects.create(name=name,age=18,ut_id=1)
# models.UserInfo.objects.create(name="方少伟",age=18,ut_id=1),
# models.UserInfo.objects.create(name="刘庚",age=18,ut_id=2),
# models.UserInfo.objects.create(name="尤勤兵",age=18,ut_id=3),
# models.UserInfo.objects.create(name="王哲",age=18,ut_id=1),
# models.UserInfo.objects.create(name="陈涛",age=18,ut_id=2),
# models.UserInfo.objects.create(name="小白",age=18,ut_id=3), # models.UserType.objects.create(title="普通用户")
# models.UserType.objects.create(title="二逼用户")
# models.UserType.objects.create(title="牛逼用户")
# return HttpResponse('ok') ##========================================正向查找===============================================
# result = models.UserInfo.objects.all()
# for obj in result:
# print(obj.name,obj.age,obj.ut_id,obj.ut.title)
##打印obj对象的名字,对象的年龄,对象的外键,ut设置外键的列就是相当于ut表对应的主键(现在拿到就是ut那个表可以拿ut对应的一行所有内容)
# result = models.UserInfo.objects.all().first()
# ##获取表中所有的行但只是拿第一个对象,也就是第一行
# print(result.name,result.age,result.ut) ##========================================反向查找===============================================
# obj = models.UserType.objects.all().last()
# print('用户类型',obj)
# for row in obj.userinfo_set.all():
# print(row.name,row.age) ##========================================对象查看及连表===============================================
# models.UserInfo.objects.all()
#查看UserIinfo表的所有行
# models.UserInfo.objects.filter(id=2)
##查看UserInfo表id等于2的哪一行内容
# result = models.UserInfo.objects.all()
##通过此方式返回一个列表中存放的是对象(object)
# for row in result:
# print(row.name,row.id,row.age,row.ut.title)
##以是对象的方式进行连表 ##========================================字典查看及连表===============================================
# models.UserInfo.objects.all()
## 查看表的所有行
# models.UserInfo.objects.filter(id=1)
##查看表的id是1的行
# result=models.UserInfo.objects.all().values('id','name','ut__title')
##通过此方式连表返回个列表,列表中存放的是字典对象(dict)
# for row in result:
# print(row) ##========================================元组查看及连表===============================================
# models.UserInfo.objects.all()
## 查看表的所有行
# models.UserInfo.objects.filter(id=2)
##查看表的id是2的行
# result=models.UserInfo.objects.all().values_list('id','name','ut__title')
##通过此方式连表返回个列表,列表中存放的是元组对象(tuple)
# for row in result:
# print(row)