我无法使用 django-celery-beat 周期性任务为模型创建对象

问题描述

这些是我的文件

app/tasks.py

from celery import shared_task
from .models import news
@shared_task
def add_title():
    new = news.object.create()
    new.title = "got it"
    new.save()

app/models.py

from django.db import models

# Create your models here.
class news(models.Model):
    title = models.CharField(max_length=10) 

app/views.py

from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.
def Save(request):
    return HttpResponse('<h1>ok</h1>')

/celery.py

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE','smallproject.settings')

app = Celery('smallproject')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings',namespace='CELERY')


app.conf.beat_schedule ={
    'add-data-to-db':{
        'task': 'app.tasks.add_title','schedule':20,}
}




# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

/settings.py 中,这些是修改/更新的内容

INSTALLED_APPS = [
    'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','app','django_celery_beat',]
CELERY_broKER_URL='redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND='redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT=['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'

我已经用这个命令来运行周期性任务(worker) celery -A smallproject beat -l INFO

问题是当我检查新闻表时,没有标题为“得到它”的记录(它是一个空表)。怎么解决..

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...