I use django-money, then I ran celery beat to update currency exchange rates every 60 minutes with the code below. *I followed django-money doc:
# "core/celery.py"
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
app = Celery('core')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
# "core/tasks.py"
from celery import shared_task
from djmoney import settings
from django.utils.module_loading import import_string
@shared_task
def update_rates(backend=settings.EXCHANGE_BACKEND, **kwargs):
backend = import_string(backend)()
backend.update_rates(**kwargs)
print("Successfully updated")
# "core/settings.py"
from celery.schedules import crontab
OPEN_EXCHANGE_RATES_APP_ID = '5507ba2d9b8f4c46adca3169aef9c281' # Here
CELERY_BEAT_SCHEDULE = {
'update_rates': {
'task': 'core.tasks.update_rates',
'schedule': crontab(minute='*/60'),
'kwargs': {} # For custom arguments
}
}
But, I got the error below even though I set OPEN_EXCHANGE_RATES_APP_ID
in settings.py
as shown above:
django.core.exceptions.ImproperlyConfigured: settings.OPEN_EXCHANGE_RATES_APP_ID should be set to use OpenExchangeRatesBackend
So, how can I solve the error to update currency exchange rates every 60 minutes with celery beat?
You need to pass
OPEN_EXCHANGE_RATES_APP_ID
toupdate_rates
task with'args'
insettings.py
as shown below. *Actually, the celery beat example to update currency exchange rates in django-money doc is old:Then,
update_rates
task needs to receiveOPEN_EXCHANGE_RATES_APP_ID
withapp_id
, then assign it toaccess_key
intasks.py
as shown below, then you can solve the error, then update currency exchange rates every 60 minutes with celery beat. *You can see openexchangerates.py on GitHub to know whyOPEN_EXCHANGE_RATES_APP_ID
needs to be assigned toaccess_key
:In addition,
OPEN_EXCHANGE_RATES_APP_ID
can be defined after the celery beat code insettings.py
as shown below without any errors and I recommend to importcrontab
just before the celery beat code as shown below so that no errors occur:And, you can use the new celery beat code on
settings.py
as shown below. *app
andupdate_rates
task need to be imported fromcore/celery.py
andcore/tasks.py
respectively as shown below and I recommend to importcrontab
,app
andupdate_rates
just before the new celery beat code as shown below so that no errors occur: