小编aka*_*eep的帖子

Django Q 对象 vs python 代码性能更好?

在 django ORM 中使用 Q 过滤条件或者简单地获取未过滤的对象并在 python 中进行比较,什么会提供更好的性能。

employee_qs = employee.objects.filter(state=States.ACTIVE, topic_assn__topic_id=instance.ss_topic_id).select_related('c_data').filter(
                Q(c_data__is_null=True) | Q(c_budget__gt=F('c_data__budget_spent') + offset_amt))
Run Code Online (Sandbox Code Playgroud)

电压/秒


employee_qs = employee.objects.filter(state=States.ACTIVE, topic_assn__topic_id=instance.ss_topic_id).select_related('c_data')
for employee in employee_qs:
    if not employee.c_data or float(employee.budget)-employee.c_data.budget_spent > offset_amt:
        #do something...
Run Code Online (Sandbox Code Playgroud)

这两个选择中哪一个的性能会更好?

python django django-models django-orm

4
推荐指数
1
解决办法
297
查看次数

标签 统计

django ×1

django-models ×1

django-orm ×1

python ×1