Using django models inside celery tasks









up vote
0
down vote

favorite












I have a use case, where I need to import some of my django models inside the celery.py file in a django-celery standard project.



My project looks like follows:



|-- backend
| |-- backend
| | |-- __init__.py
| | |-- celery.py
| | |-- settings.py
| | |-- urls.py
| | `-- wsgi.py
| |-- bobbers
| | |-- __init__.py
| | |-- admin.py
| | |-- apps.py
| | |-- models.py
| | |-- serializers.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- manage.py
| |-- requirements.txt


All the contents of the celery related files (backend/backend/__init__.py, and backend/backend/celery.py) are exactly as indicated in the official docs.



But once I add the line import bobbers.models as bobber_models to my celery.py file, I get the infamous AppRegistryNotReady error with the following stacktrace:



 File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 87, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 132, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.


Needless to say, if I remove the import, all goes well.



So how can I safely import django models in the same file where I write my celery tasks?










share|improve this question





















  • The question is why do you need to do this?
    – Juan Ignacio Sánchez
    Nov 10 at 15:27










  • Because my tasks do some heavy calculation and update parameters in the models. They also need model's attributes for the calculations
    – bluesummers
    Nov 10 at 15:40










  • Can you provide some high-level example? i mean, without code.
    – Juan Ignacio Sánchez
    Nov 10 at 16:13











  • I have a one to many relationship between two models, A and B. B has a numeric value. A has millions of B. I want to asynchronically update some parameter X of A that depends on the numeric values of B and the updating is a complex operation
    – bluesummers
    Nov 10 at 17:12










  • why don't you define a tasks.py as suggested in here: docs.celeryproject.org/en/latest/getting-started/…
    – Juan Ignacio Sánchez
    Nov 10 at 17:27














up vote
0
down vote

favorite












I have a use case, where I need to import some of my django models inside the celery.py file in a django-celery standard project.



My project looks like follows:



|-- backend
| |-- backend
| | |-- __init__.py
| | |-- celery.py
| | |-- settings.py
| | |-- urls.py
| | `-- wsgi.py
| |-- bobbers
| | |-- __init__.py
| | |-- admin.py
| | |-- apps.py
| | |-- models.py
| | |-- serializers.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- manage.py
| |-- requirements.txt


All the contents of the celery related files (backend/backend/__init__.py, and backend/backend/celery.py) are exactly as indicated in the official docs.



But once I add the line import bobbers.models as bobber_models to my celery.py file, I get the infamous AppRegistryNotReady error with the following stacktrace:



 File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 87, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 132, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.


Needless to say, if I remove the import, all goes well.



So how can I safely import django models in the same file where I write my celery tasks?










share|improve this question





















  • The question is why do you need to do this?
    – Juan Ignacio Sánchez
    Nov 10 at 15:27










  • Because my tasks do some heavy calculation and update parameters in the models. They also need model's attributes for the calculations
    – bluesummers
    Nov 10 at 15:40










  • Can you provide some high-level example? i mean, without code.
    – Juan Ignacio Sánchez
    Nov 10 at 16:13











  • I have a one to many relationship between two models, A and B. B has a numeric value. A has millions of B. I want to asynchronically update some parameter X of A that depends on the numeric values of B and the updating is a complex operation
    – bluesummers
    Nov 10 at 17:12










  • why don't you define a tasks.py as suggested in here: docs.celeryproject.org/en/latest/getting-started/…
    – Juan Ignacio Sánchez
    Nov 10 at 17:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a use case, where I need to import some of my django models inside the celery.py file in a django-celery standard project.



My project looks like follows:



|-- backend
| |-- backend
| | |-- __init__.py
| | |-- celery.py
| | |-- settings.py
| | |-- urls.py
| | `-- wsgi.py
| |-- bobbers
| | |-- __init__.py
| | |-- admin.py
| | |-- apps.py
| | |-- models.py
| | |-- serializers.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- manage.py
| |-- requirements.txt


All the contents of the celery related files (backend/backend/__init__.py, and backend/backend/celery.py) are exactly as indicated in the official docs.



But once I add the line import bobbers.models as bobber_models to my celery.py file, I get the infamous AppRegistryNotReady error with the following stacktrace:



 File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 87, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 132, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.


Needless to say, if I remove the import, all goes well.



So how can I safely import django models in the same file where I write my celery tasks?










share|improve this question













I have a use case, where I need to import some of my django models inside the celery.py file in a django-celery standard project.



My project looks like follows:



|-- backend
| |-- backend
| | |-- __init__.py
| | |-- celery.py
| | |-- settings.py
| | |-- urls.py
| | `-- wsgi.py
| |-- bobbers
| | |-- __init__.py
| | |-- admin.py
| | |-- apps.py
| | |-- models.py
| | |-- serializers.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| |-- manage.py
| |-- requirements.txt


All the contents of the celery related files (backend/backend/__init__.py, and backend/backend/celery.py) are exactly as indicated in the official docs.



But once I add the line import bobbers.models as bobber_models to my celery.py file, I get the infamous AppRegistryNotReady error with the following stacktrace:



 File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 87, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 132, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.


Needless to say, if I remove the import, all goes well.



So how can I safely import django models in the same file where I write my celery tasks?







python django-celery






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 15:21









bluesummers

2,01611943




2,01611943











  • The question is why do you need to do this?
    – Juan Ignacio Sánchez
    Nov 10 at 15:27










  • Because my tasks do some heavy calculation and update parameters in the models. They also need model's attributes for the calculations
    – bluesummers
    Nov 10 at 15:40










  • Can you provide some high-level example? i mean, without code.
    – Juan Ignacio Sánchez
    Nov 10 at 16:13











  • I have a one to many relationship between two models, A and B. B has a numeric value. A has millions of B. I want to asynchronically update some parameter X of A that depends on the numeric values of B and the updating is a complex operation
    – bluesummers
    Nov 10 at 17:12










  • why don't you define a tasks.py as suggested in here: docs.celeryproject.org/en/latest/getting-started/…
    – Juan Ignacio Sánchez
    Nov 10 at 17:27
















  • The question is why do you need to do this?
    – Juan Ignacio Sánchez
    Nov 10 at 15:27










  • Because my tasks do some heavy calculation and update parameters in the models. They also need model's attributes for the calculations
    – bluesummers
    Nov 10 at 15:40










  • Can you provide some high-level example? i mean, without code.
    – Juan Ignacio Sánchez
    Nov 10 at 16:13











  • I have a one to many relationship between two models, A and B. B has a numeric value. A has millions of B. I want to asynchronically update some parameter X of A that depends on the numeric values of B and the updating is a complex operation
    – bluesummers
    Nov 10 at 17:12










  • why don't you define a tasks.py as suggested in here: docs.celeryproject.org/en/latest/getting-started/…
    – Juan Ignacio Sánchez
    Nov 10 at 17:27















The question is why do you need to do this?
– Juan Ignacio Sánchez
Nov 10 at 15:27




The question is why do you need to do this?
– Juan Ignacio Sánchez
Nov 10 at 15:27












Because my tasks do some heavy calculation and update parameters in the models. They also need model's attributes for the calculations
– bluesummers
Nov 10 at 15:40




Because my tasks do some heavy calculation and update parameters in the models. They also need model's attributes for the calculations
– bluesummers
Nov 10 at 15:40












Can you provide some high-level example? i mean, without code.
– Juan Ignacio Sánchez
Nov 10 at 16:13





Can you provide some high-level example? i mean, without code.
– Juan Ignacio Sánchez
Nov 10 at 16:13













I have a one to many relationship between two models, A and B. B has a numeric value. A has millions of B. I want to asynchronically update some parameter X of A that depends on the numeric values of B and the updating is a complex operation
– bluesummers
Nov 10 at 17:12




I have a one to many relationship between two models, A and B. B has a numeric value. A has millions of B. I want to asynchronically update some parameter X of A that depends on the numeric values of B and the updating is a complex operation
– bluesummers
Nov 10 at 17:12












why don't you define a tasks.py as suggested in here: docs.celeryproject.org/en/latest/getting-started/…
– Juan Ignacio Sánchez
Nov 10 at 17:27




why don't you define a tasks.py as suggested in here: docs.celeryproject.org/en/latest/getting-started/…
– Juan Ignacio Sánchez
Nov 10 at 17:27

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240364%2fusing-django-models-inside-celery-tasks%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240364%2fusing-django-models-inside-celery-tasks%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3