也许不严谨,仅供自己学习和研究使用
get
get查询返回
使用get查询返回的是一个对象
resp = oauth_clients.objects.get(appid=body_data['appid'])
print(type(resp))
print(resp)
如果有结果,则返回结果是
<class 'oauth2.models.oauth_clients'>
{"id": 1, "appid": 100001, "secret": "9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi", "name": "\u6d4b\u8bd51", "created_time": "2020-04-18 21:36:00", "update_time": "2020-04-18 21:36:00"}
查询为空的情况
一般如果get查询数据为空,那么django就会抛出异常
oauth2.models.oauth_clients.DoesNotExist: oauth_clients matching query does not exist.
这里为了防止报错,我们就需要做一下异常处理,判断一下返回就行了
from .models import oauth_clients
try:
resp = oauth_clients.objects.get(appid=body_data['appid'])
except oauth_clients.DoesNotExist: # 这里的except需要捕获oauth_clients的异常,也就上面导入的oauth_clients
response_data['result'] = '0'
return HttpResponse(json.dumps(response_data), content_type="application/json")
get返回信息提取
如何把查询的<class 'oauth2.models.oauth_clients'>
这个结果里的信息提取出来呢
直接 resp.appid
即可
resp = oauth_clients.objects.get(appid=body_data['appid'])
print(type(resp))
print(resp.appid) # 这个appid 是数据库的appid的字段。和model中return返回无关
# 返回
<class 'oauth2.models.oauth_clients'>
100001
假如:
我设置model返回为appidd
或者我设置 model返回一个数组
我使用get查询
try:
resp = oauth_clients.objects.get(appid=body_data['appid'])
print(resp)
print(resp.appid)
print(resp.secret)
except oauth_clients.DoesNotExist:
print('报错')
使用resp.appid
提取,还是可以正常提取到内容
{"id": 1, "appidd": 100001, "secret": "9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi", "name": "\u6d4b\u8bd51", "created_time": "2020-04-18 21:36:00", "update_time": "2020-04-18 21:36:00"}
100001
9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi
说明结论:
使用get提取字段内容。是自己提取查询出来的数据库的字段的,和model定义的返回格式是没有关系的
数据返回说明
为什么查询结果会返回这样的json格式的字符串呢?
为了美观返回的数据,我在定义model的时候设置了格式
class oauth_clients(models.Model):
id = models.IntegerField(primary_key=True)
appid = models.IntegerField(null=True)
secret = models.CharField(max_length=128,null=True,verbose_name='秘钥')
name = models.CharField(max_length=128,null=True,verbose_name='备注')
created_time = models.DateTimeField(null=True)
update_time = models.DateTimeField(null=True)
class Meta:
db_table = 'api_oauth'
# 定义返回的数据为json格式的字符串
def __str__(self):
return json.dumps({
'id':self.id,
'appid':self.appid,
'secret':self.secret,
'name':self.name,
'created_time':datetime.strftime(self.created_time,"%Y-%m-%d %H:%M:%S"),
'update_time': datetime.strftime(self.update_time, "%Y-%m-%d %H:%M:%S"),
})
因为的model上面定义了返回是json格式的数据,因此我get到的结果也是json格式的字符串
这个只是json格式的字符串,不是json格式的数据
{"id": 1, "appid": 100001, "secret": "9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi", "name": "\u6d4b\u8bd51", "created_time": "2020-04-18 21:36:00", "update_time": "2020-04-18 21:36:00"}
filter
filter返回的是一个QuerySet,
resp = oauth_clients.objects.filter(appid=body_data['appid'])
print(type(resp))
print(resp)
如果有结果,则返回结果是 这个QuerySet应该是一个列表,可能有多条数据,我只提取一条
<class 'django.db.models.query.QuerySet'>
<QuerySet [<oauth_clients: {"id": 1, "appid": 100001}>]>
是可以直接使用[0]提取列表中内容
resp = oauth_clients.objects.filter(appid=body_data['appid'])
if len(resp) > 0:
print(resp[0])
print(type(resp[0]))
因为filter查询不到结果不会报错,没有len(resp)为空。有结果len(resp)会返回结果条数,
查询结果
(100001, '9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi')
<class 'oauth2.models.oauth_clients'>
这个结果返回是根据定义的model-return返回的
下面是我定义的return返回结果。
但是我对filter
的QuerySet
进行字典化,得到的结果和我model中return
不一样
from django.forms.models import model_to_dict, #model_to_dict把QuerySet结果转换为字典
resp = oauth_clients.objects.filter(appid=body_data['appid'])
if len(resp) > 0:
print(resp[0])
print(type(resp[0]))
print(type(model_to_dict(resp[0])))
print(model_to_dict(resp[0]))
model_to_dict(resp[0])
结果是返回整个数据库的字段
(100001, '9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi')
<class 'oauth2.models.oauth_clients'>
<class 'dict'>
{'id': 1, 'appid': 100001, 'secret': '9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi', 'name': '测试1', 'created_time': datetime.datetime(2020, 4, 18, 21, 36), 'update_time': datetime.datetime(2020,
4, 18, 21, 36)}
下面我改回来,准备返回为json
格式的字符串。
代码:
body_data = json.loads(data)
resp = oauth_clients.objects.filter(appid=body_data['appid'])
if len(resp) > 0:
print(resp[0])
print(type(resp[0]))
print(type(model_to_dict(resp[0])))
print(model_to_dict(resp[0]))
结果:
第一次resp[0]
返回的是 我在model
中定义的return
。
第二次model_to_dict(resp[0])
返回的是数据库的字段拼接的json
格式
{"id": 1, "appidd": 100001, "secret": "9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi", "name": "\u6d4b\u8bd51", "created_time": "2020-04-18 21:36:00", "update_time": "2020-04-18 21:36:00"}
<class 'oauth2.models.oauth_clients'>
<class 'dict'>
{'id': 1, 'appid': 100001, 'secret': '9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi', 'name': '测试1', 'created_time': datetime.datetime(2020, 4, 18, 21, 36), 'update_time': datetime.datetime(2020,
4, 18, 21, 36)}
model_to_dict
可以把QuerySet
结果转换为字典
from django.forms.models import model_to_dict
resp = oauth_clients.objects.filter(appid=body_data['appid'])
if len(resp) > 0:
print(model_to_dict(resp[0]))
如果查询 appid,则可以正常返回数据
{"id": 1, "appidd": 100001, "secret": "9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi", "name": "\u6d4b\u8bd51", "created_time": "2020-04-18 21:36:00", "update_time": "2020-04-18 21:36:00"}
<class 'oauth2.models.oauth_clients'>
<class 'dict'>
{'id': 1, 'appid': 100001, 'secret': '9v4ijOcy9x7TCOgCU5nLYXTm14n9rGbi', 'name': '测试1', 'created_time': datetime.datetime(2020, 4, 18, 21, 36), 'update_time': datetime.datetime(2020,
4, 18, 21, 36)}
100001
2020-04-18 21:36:00
但是如果查询appidd,就会报错
说明还是从数据库中取值,而不是model中return的值