如何使用celery / cronjobs每隔5分钟在数据库中添加一个用户

时间:2015-10-12 09:05:37

标签: python django celery crontab

如何使用celery / cronjobs每隔5分钟在数据库中添加一个用户?

我的代码无效:

@periodic_task(run_every=crontab( minute='5', day_of_week="*"))
def create_user():
    try
        user = registrations.objects.get(username='username',password='password')
    except User.DoesNotExist:
        user = registrations(username='username', password='password')
        user.save()

1 个答案:

答案 0 :(得分:-1)

我猜你可以做这样的事情。

from uuid import uuid4

@periodic_task(run_every=crontab(minute='*/5'))
def create_user():
    try:
        username = uuid4().hex # username should be unique
        user = registrations(username=username,password='pass123')
        user.save()
        # user = User.objects.create(username=username,password='pass123')  # If you are using Django auth user model, which you should. No need to call .save() on this
    except Exception, e:
        print str(e)  # Log the error