djang4o查询某个字段的值

# -*- coding:utf-8 -*-
import os,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'dbtest.settings'

import django
django.setup()


from db.models import asset,server

#获取标的某个字段的值
ret = asset.objects.all() # 不会去连接数据库

# 当真正用的时候才去连接数据库
for r in ret:
print(r.sn)
# 建立了一个tcp连接,执行的sql语句:
# SELECT `db_asset`.`id`, `db_asset`.`sn`, `db_asset`.`money`, `db_asset`.`addtime` FROM `db_asset`; args=()


ret1 = asset.objects.values('sn','money').get(sn='cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c3')
print(ret1)
# (0.001) SELECT `db_asset`.`sn`, `db_asset`.`money` FROM `db_asset` WHERE `db_asset`.`sn` = 'cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c3'; args=('cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c3',)
# {'money': 5000.0, 'sn': u'cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c3'}


# ret2 = asset.objects.get(sn='cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c3').values('sn','money') # 错误
# print(ret2)

ret3 = asset.objects.values('sn','money').filter(money=5000)
print(ret3)
# (0.001) SELECT `db_asset`.`sn`, `db_asset`.`money` FROM `db_asset` WHERE `db_asset`.`money` = 5000 LIMIT 21; args=(5000.0,)
# <QuerySet [{'money': 5000.0, 'sn': u'cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c3'}]>

ret4 = asset.objects.values('sn','money')
print(ret4)
# (0.001) SELECT `db_asset`.`sn`, `db_asset`.`money` FROM `db_asset` LIMIT 21; args=()
# <QuerySet [{'money': 1000.0, 'sn': u'cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c8'}, {'money': 2000.0, 'sn': u'cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c1'}, {'money': 1500.0, 'sn': u'cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c2'}, {'mo
# ney': 5000.0, 'sn': u'cb984b6b-0ef9-46ea-b7be-dbe68eb4f2c3'}]>
posted @ 2016-10-20 16:22  电神  阅读(329)  评论(0编辑  收藏  举报