Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django数据库查询往返_Python_Django_Django Models - Fatal编程技术网

Python Django数据库查询往返

Python Django数据库查询往返,python,django,django-models,Python,Django,Django Models,我有下面的查询,您可以在我的循环中看到,我添加了每条消息。我想减少往返DB的总次数。这是一种我可以一次处理成批创建的消息(比如说20条)的方法吗?这有助于提高速度吗?欢迎任何建议 class ProcessRequests(Task): """ Celery Task to start request to process that are not scheduled. """ name = "Request to Process" max_retries

我有下面的查询,您可以在我的循环中看到,我添加了每条消息。我想减少往返DB的总次数。这是一种我可以一次处理成批创建的消息(比如说20条)的方法吗?这有助于提高速度吗?欢迎任何建议

class ProcessRequests(Task):
    """
    Celery Task to start request to process that are not scheduled.
    """
    name = "Request to Process"
    max_retries = 1
    default_retry_delay = 3

    def run(self, batch):
        # Only run this task on non-scheduled tasks
        if batch.status != "Scheduled":
            q = Contact.objects.filter(contact_owner=batch.user, subscribed=True)
            if batch.group == None:
                q = q.filter(id=batch.contact_id)
            else:
                q = q.filter(group=batch.group)

            for e in q:
                msg = Message.objects.create(
                    recipient_number=e.mobile,
                    content=batch.content,
                    sender=e.contact_owner,
                    billee=batch.user,
                    sender_name=batch.sender_name
                )
                gateway = Gateway.objects.get(pk=2)
                msg.send(gateway)
你可以用


还要注意的是,每次通过循环都会得到相同的
网关
对象,最好在循环之外获得一次,然后每次都使用相同的对象。

感谢您的帮助:)