sql - 从外部源查询数据的 django 模型

标签 sql django postgresql django-models django-queryset

给定一个 django 模型...

models.py

class UserRelationship:
    user_id - IntegerField
    staff_id - IntegerField
    valid_from - DateTimeField

...以及一些从外部 API 检索数据的逻辑。

api.py

class Approval:
    user_id - Int
    created_at - DateTime

带有“批准”列表:

approvals = [{'user_id': <user_id>, 'created_at': <created_at>}, ...]

我需要找到一种有效的方法来在批准“批准”对象列表时派生“staff_id”。

我想不出使用 Django ORM 执行此操作的方法。

我知道我们可以使用 Q 对象进行复杂的查找:

from django.db.models import Q

qs = UserRelationship.obejcts.filter(Q(user_id=<user_id>) & Q(created_at__lte=<created_at>))

但这仅适用于 user_id/created_at 的单个组合,我如何才能为“批准”的大列表(~20k+)执行此操作。

任何帮助或提示将不胜感激。非常感谢。

最佳答案

Given a list of "approvals" (derived from an external source) e.g

   approvals = [{'user_id': <user_id>, 'created_at': <created_at>}, ...]

I need find an efficient way to find "staff_id" at the time of approval for a list of ~20k+ "approval" objects.

i.e for each dict find a matching row where

   approval.user_id = user_relationship.user_id and approval.created_at <= user_relationship.valid_from

根据您的外部数据源、索引等,效率会很重要。但是对于您关于如何制定查询的直接问题,best place to start is with django.db.models.Q :

If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.

A Q object (django.db.models.Q) is an object used to encapsulate a collection of keyword arguments. These keyword arguments are specified as in “Field lookups” above.

filters = Q()
for x in approvals:
    filters |= Q(user_id=x['user_id'], valid_from__lte=x['created_at'])
relationships = UserRelationship.objects.filter(filers)

并且您可以通过遍历 relationships 查询集来获取 staff_id。此示例假定您在批准列表中具有唯一的 user_id,以便您可以返回并将正确的批准与正确的 staff_id 相关联。如果批准列表中可以有多个相同的 user_id,则只需按照 user_id 在每个分区中出现一次以上的方式对批准进行分区。

partitions = []
check_ids = []
for x in approvals:
    current_partition = None
    current_check_id = None
    for partition, check_id in zip(partitions, check_ids):
        if x['user_id'] not in check_id:
            current_partition = partition
            current_check_id = check_id

    if current_partition is None:
        partitions.append(list())
        check_ids.append(set())
        current_partition = partitions[-1]
        current_check_id = check_ids[-1]
    current_check_id.add(x['user_id']
    current_partition.append(x)

关于sql - 从外部源查询数据的 django 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41946986/

相关文章:

python - 如何在服务器端发出 Socket IO 事件

Django 对象在查询集中不可迭代

python - 使用 sudo 运行 Django 找不到已安装的 Homebrew 程序包

postgresql - 将 Docker Postgres 容器连接到现有的空数据库

postgresql - 我无法在 Mac Big Sur 上的 MAMP 中添加 pgsql PDO 驱动程序

asp.net - 出现错误 "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

sql - 在更新语句中具有选择子查询的嵌套 SQL 查询

mysql - 基于单元格值的动态列选择

sql - 我想在 SQL 中使用 Recursive 返回新列?

sql - 我应该在查找表中使用代码吗