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)
在这里,我正在使用芹菜向用户模型插入一些随机数据 并且可以在获取相同数据的同时工作。
但是,我想从数据库中获取“现有数据”,而无需在相同请求下插入它们。 请分享一些想法,我该怎么做。
答案 0 :(得分:0)
方法1插入destring state, generate(statenum) float
state: contains nonnumeric characters; no **generate**
,然后通过POST
进行检索:
GET
方法2只是删除对create_random_user_accounts的调用,因为这就是创建帐户的方法:
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)