python - How do i set django setting to a file which is outside the django tree? -


i have python file tasks.py

import os     os.environ.setdefault('django_settings_module', 'djangowebproject.settings')     django.contrib.auth.models import user     logreg.models import activationcode     import datetime      def remove_users():         print 'hello worldddddddddddddddddddddddddddddddddd'         inactive_users = []         activation_codes = activationcode.objects.all()         activation_code in activation_codes:             if datetime.datetime.date(activation_code.key_expires) < datetime.datetime.date(datetime.datetime.now()):                 inactive_users.append(activation_code.user_id)          inactive_user in inactive_users:             user.objects.filter(id=inactive_user).delete() 

but in root folder , when try execute it, gives me following error

file "c:\users\deybala1\appdata\local\continuum\anaconda2\lib\site-packages\dj ango\apps\registry.py", line 124, in check_apps_ready raise appregistrynotready("apps aren't loaded yet.") django.core.exceptions.appregistrynotready: apps aren't loaded yet.

how fix this?

if you're creating script using django project, absolutely necessary set path settings of project before import django or project. , you're importing user model django in 1st line , model project in second.

also, need call django.setup() first.

to fix that, move import os , setting path django settings beginning of script, , put django.setup() after (with proper import), this:

# first, set path project settings import os os.environ.setdefault('django_settings_module', 'djangowebproject.settings') import django django.setup()  # can import else django.contrib.auth.models import user logreg.models import activationcode import datetime  def remove_users():     print 'hello worldddddddddddddddddddddddddddddddddd'     inactive_users = []     activation_codes = activationcode.objects.all()     activation_code in activation_codes:         if datetime.datetime.date(activation_code.key_expires) < datetime.datetime.date(datetime.datetime.now()):             inactive_users.append(activation_code.user_id)      inactive_user in inactive_users:         user.objects.filter(id=inactive_user).delete() 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -