Django ORM Query 输出格式

Django ORM Query 输出格式

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Schedule.settings')
django.setup()

model

class User(models.Model):
    """
    用户
    """
    username = models.CharField('用户名', max_length=50, unique=True)
    password = models.CharField('密码', max_length=256, default='')
    email = models.EmailField('邮箱', max_length=255)
    phone = models.CharField('电话', max_length=13, default='',help_text='字段备注说明')
    dept_id = models.IntegerField('部门id', default=0)
    is_admin = models.BooleanField('超级管理员', default=False)
    gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
    gmt_modified = models.DateTimeField('更新时间', auto_now=True)
    is_deleted = models.BooleanField('已删除', default=False)

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'users'

ORM

users = User.objects.first()

返回用户表中的第一个

users = User.objects.first()
print(users) #User object (1)
print(users.username) #admin

users = User.objects.latest(‘id’)

lastest(*args),取最新的一个对象

users = User.objects.filter(depart='软件').latest('phone')
print(users) #User object (4)

users = User.objects.get(id=1)

返回与所筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或者没有都会抛出错误

users = User.objects.get(id=1)
print(users)
# User object (1)
-----------------------------------------------
print(users.username)
# admin
-----------------------------------------------
print(type(users))
# <class 'users.models.User'>

users = User.objects.all()

查询所有结果

users = User.objects.all()
print(users)  
# <QuerySet [<User: User object (1)>, <User: User object (2)>, <User: User object (3)>, <User: User object (4)>, <User: User object (5)>, <User: User object (6)>, <User: User object (7)>]>
-----------------------------------------------
print(type(users))
# <class 'django.db.models.query.QuerySet'>

users = User.objects.values()

返回一个QuerySet集合——— 一个特殊的QuerySet,运行后得到的并不是一系列Model的实例化对象,而是一个可迭代的字典序列

users = User.objects.values()
print(users)
# <QuerySet [{'id': 1, 'username': 'admin', 'password': 'pbkdf2_sha256$100000$MYl4ILvcFJ63$ahZhbLRcuWvwKslqTtrO8lL9L2sWafD7XhMoRnlicLg=', 'depart': '', 'phone': '17664207079', 'isadmin': True}, {'id': 2, 'username': '张嘉福', 'password': 'pbkdf2_sha256$100000$uXXDBL267NLC$NfLG5BZ6AjAsbNJsDYhhl/zA55cYXohYJuI4OmsBQx4=', 'depart': '软件', 'phone': '17664207077', 'isadmin': False}, {'id': 3, 'username': '王轩', 'password': 'pbkdf2_sha256$100000$VZFhrBSmIr8L$RFXYZo2+i47yYH1HwWqQxlwhy+e+2NYUVvtdnBee/u8=', 'depart': '软件', 'phone': '17664207068', 'isadmin': False}, {'id': 4, 'username': '路宇', 'password': 'pbkdf2_sha256$100000$GAQapzxEKF3t$pdIXEcmSrAcE3WvMWtfF5luCswj+GQosyR4/Zp1SmXc=', 'depart': '软件', 'phone': '17664207088', 'isadmin': False}, {'id': 5, 'username': '李婷婷', 'password': 'pbkdf2_sha256$100000$zn2DYNts9UOD$qBFz2Pq2Wo2vJn1mZcbezoAtCxC3JtMIL8VUhFqe1p8=', 'depart': '网络', 'phone': '17664207089', 'isadmin': False}, {'id': 6, 'username': '李康', 'password': 'pbkdf2_sha256$100000$0oeg7bsI5jj4$kq283rcHReyyHvfPKCOdtILVllnIyeM9AZTAkZY6svk=', 'depart': '网络', 'phone': '17664207099', 'isadmin': False}, {'id': 7, 'username': '王泽星', 'password': 'pbkdf2_sha256$100000$7hZy8RFR62Wo$NDnXlP+5gEtNa0wSShTCDfIl6RFhMUZBpPTK901ZdH0=', 'depart': '网络', 'phone': '17664207010', 'isadmin': False}]>
------------------------------------------------------
print(type(users))
# <class 'django.db.models.query.QuerySet'>
users = User.objects.values('username')
print(users)
# <QuerySet [{'username': 'admin'}, {'username': '张嘉福'}, {'username': '李婷婷'}, {'username': '李康'}, {'username': '王泽星'}, {'username': '王轩'}, {'username': '路宇'}]>
print(type(users))
# <class 'django.db.models.query.QuerySet'>
--------------------------------------------------------
print(users[0])
# {'username': 'admin'}
print(type(users[0]))
# <class 'dict'>

users = User.objects.filter(**kwargs)

筛选与条件所匹配的对象,返回的是一个对象查询集合(无参数默认返回全部集合)

user1 = User.objects.filter(id=1)
print('11111', user1) #11111 <QuerySet [<User: User object (1)>]>
user2 = User.objects.filter(id=1).all()
print('22222', user2) #22222 <QuerySet [<User: User object (1)>]>
user3 = User.objects.filter(id=1).first()
print('33333', user3) #33333 User object (1)

-------------------------------------------------------------------------------
users = User.objects.filter()
print(users)  
# <QuerySet [<User: User object (1)>, <User: User object (2)>, <User: User object (3)>, <User: User object (4)>, <User: User object (5)>, <User: User object (6)>, <User: User object (7)>]>

print(type(users))
# <class 'django.db.models.query.QuerySet'>
def __str__(self):
	return self.username
# users = User.objects.filter().all()
# print(users)  #<QuerySet [<User: admin>, <User: 张嘉福>, <User: 王轩>, <User: 路宇>, <User: 李婷婷>, <User: 李康>, <User: 王泽星>]>

users = User.objects.values(‘depart’).annotate(count=Count(‘depart’)).all()

users1 = User.objects.values('depart')
users2 = User.objects.values('depart').annotate(count=Count('depart')).all()
print('11111', users1)#11111 <QuerySet [{'depart': '软件'}, {'depart': '软件'}, {'depart': '软件'}, {'depart': '网络'}, {'depart': '网络'}, {'depart': '网络'}]>
print('22222', users2)#22222 <QuerySet [{'depart': '软件', 'count': 3}, {'depart': '网络', 'count': 3}]>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值