我如何从以前存在的数据中使用芹菜(异步)从django模型(数据库)中获取数据
How can i fetch data from django models(database) using celery(asynchronously) from previously existing data
tasks.py
import string
from django.contrib.auth.models import User
from django.utils.crypto import get_random_string
from celery import shared_task
@shared_task
def create_random_user_accounts(total):
for i in range(total):
username = 'user_{}'.format(get_random_string(10, string.ascii_letters))
email = '{}@example.com'.format(username)
password = get_random_string(50)
User.objects.create_user(username=username, email=email, password=password)
return '{} random users created with success!'.format(total)
views.py
from django.contrib.auth.models import User
from .tasks import create_random_user_accounts
from django.http import JsonResponse
def users(request):
obj = list(User.objects.values())
create_random_user_accounts.delay(20)
return JsonResponse(obj,safe=False)
这里我使用芹菜向用户模型插入一些随机数据
它在获取相同数据的同时工作。
但是,我想根据同一个请求从数据库 'without inseting' 中获取 'existing data'。
请分享一些想法,我该怎么做。
方法 #1 从 POST
中插入,然后通过 GET
检索:
from django.contrib.auth.models import User
from .tasks import create_random_user_accounts
from django.http import JsonResponse
from django.views.generic import View
class UserView(View):
def get(self, request, *args, **kwargs):
obj = list(User.objects.values())
return JsonResponse(obj,safe=False)
def post(self, request, *args, **kwargs):
create_random_user_accounts.delay(20)
obj = list(User.objects.values())
return JsonResponse(obj,safe=False)
方法 #2 只是删除对 create_random_user_accounts 的调用,因为这是创建帐户的原因:
from django.contrib.auth.models import User
from .tasks import create_random_user_accounts
from django.http import JsonResponse
def users(request):
obj = list(User.objects.values())
<b># create_random_user_accounts.delay(20)</b>
return JsonResponse(obj,safe=False)
tasks.py
import string
from django.contrib.auth.models import User
from django.utils.crypto import get_random_string
from celery import shared_task
@shared_task
def create_random_user_accounts(total):
for i in range(total):
username = 'user_{}'.format(get_random_string(10, string.ascii_letters))
email = '{}@example.com'.format(username)
password = get_random_string(50)
User.objects.create_user(username=username, email=email, password=password)
return '{} random users created with success!'.format(total)
views.py
from django.contrib.auth.models import User
from .tasks import create_random_user_accounts
from django.http import JsonResponse
def users(request):
obj = list(User.objects.values())
create_random_user_accounts.delay(20)
return JsonResponse(obj,safe=False)
这里我使用芹菜向用户模型插入一些随机数据 它在获取相同数据的同时工作。
但是,我想根据同一个请求从数据库 'without inseting' 中获取 'existing data'。 请分享一些想法,我该怎么做。
方法 #1 从 POST
中插入,然后通过 GET
检索:
from django.contrib.auth.models import User
from .tasks import create_random_user_accounts
from django.http import JsonResponse
from django.views.generic import View
class UserView(View):
def get(self, request, *args, **kwargs):
obj = list(User.objects.values())
return JsonResponse(obj,safe=False)
def post(self, request, *args, **kwargs):
create_random_user_accounts.delay(20)
obj = list(User.objects.values())
return JsonResponse(obj,safe=False)
方法 #2 只是删除对 create_random_user_accounts 的调用,因为这是创建帐户的原因:
from django.contrib.auth.models import User
from .tasks import create_random_user_accounts
from django.http import JsonResponse
def users(request):
obj = list(User.objects.values())
<b># create_random_user_accounts.delay(20)</b>
return JsonResponse(obj,safe=False)