相关指令:
1. python manage.py validate 验证模型的有效性;
2. python manage.py syncdb 同步数据库,创建表;
3. python manage.py shell 往对应模块添加字段对应数据;
Django中获取指定列的数据:
1. values 查询:(返回字典)
#获取一个字段
ipList = EmployeeIP.objects.values("IP").first()
print(type(ipList))
# <class 'dict'>
print(ipList)
# {'IP': '192.168.1.41'}
#获取多个字段
empList = Employee.objects.values("first_name", "last_name", "email")[0:2]
print(type(empList))
# <class 'django.db.models.query.QuerySet'>
print(empList)
# [
# {'last_name': 'Wei', 'first_name': 'Vena', 'email': 'Vena@test.com'},
# {'last_name': 'Wan', 'first_name': 'Mark', 'email': 'mwan@test.com'}
# ]
2. values_list 查询:(返回列表)
ipList = EmployeeIP.objects.values_list("IP").first()
print(type(ipList))
# <class 'tuple'>
print(ipList)
# ('192.168.1.111',)
ipList = EmployeeIP.objects.values_list("IP")[0:2]
print(type(ipList))
# <class 'django.db.models.query.QuerySet'>
print(ipList)
# [('192.168.1.41',), ('192.168.1.44',)]
print(type(ipList[0]))
# <class 'tuple' >
print(ipList[0])
# 192.168.1.111