python - Error: django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet -


i'm trying write importer csv file. here minimal example

csv_filepathname="/home/thomas/downloads/zip.csv" your_djangoproject_home="~/desktop/projects/myproject/myproject/"  import sys,os,csv  sys.path.append(your_djangoproject_home) os.environ['django_settings_module'] = 'settings'  django.db.models.fields.related import manytomanyfield myapp.models import zipcode,state   datareader = csv.reader(open(csv_filepathname), delimiter=',', quotechar='"')     def import_so(item, crit,val):      debug = 1;      obj_state=type(item).objects.filter(crit=val)     <...some other stuff...>     return    row in datareader:      st=state(statecode=row[2],statename=row[3])      import_so(st,"statename",row[3])  

and here model

class state(models.model):     statecode = models.charfield(max_length=2, default='xx')     statename = models.charfield(max_length=32, default='xxxxxxxxxxxxx') 

when execute code is, following error:

file "load_data.py", line 101, in <module>     import_so(st,"statename",row[3]) # objekt, kriterium, zu vergleichender wert   file "load_data.py", line 68, in import_so     obj_state=type(item).objects.filter(crit=val)#crit=val) # suche object mit merkmal, hier statename   file "/usr/lib/python2.7/dist-packages/django/db/models/manager.py", line 127, in manager_method     return getattr(self.get_queryset(), name)(*args, **kwargs)   file "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 679, in filter     return self._filter_or_exclude(false, *args, **kwargs)   file "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 697, in _filter_or_exclude     clone.query.add_q(q(*args, **kwargs))   file "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1310, in add_q     clause, require_inner = self._add_q(where_part, self.used_aliases)   file "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1338, in _add_q     allow_joins=allow_joins, split_subq=split_subq,   file "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1150, in build_filter     lookups, parts, reffed_expression = self.solve_lookup_type(arg)   file "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1036, in solve_lookup_type     _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())   file "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1394, in names_to_path     field_names = list(get_field_names_from_opts(opts))   file "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 45, in get_field_names_from_opts     f in opts.get_fields()   file "/usr/lib/python2.7/dist-packages/django/db/models/options.py", line 740, in get_fields     return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)   file "/usr/lib/python2.7/dist-packages/django/db/models/options.py", line 802, in _get_fields     all_fields = self._relation_tree   file "/usr/lib/python2.7/dist-packages/django/utils/functional.py", line 59, in __get__     res = instance.__dict__[self.name] = self.func(instance)   file "/usr/lib/python2.7/dist-packages/django/db/models/options.py", line 709, in _relation_tree     return self._populate_directed_relation_graph()   file "/usr/lib/python2.7/dist-packages/django/db/models/options.py", line 681, in _populate_directed_relation_graph     all_models = self.apps.get_models(include_auto_created=true)   file "/usr/lib/python2.7/dist-packages/django/utils/lru_cache.py", line 101, in wrapper     result = user_function(*args, **kwds)   file "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 168, in get_models     self.check_models_ready()   file "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 131, in check_models_ready     raise appregistrynotready("models aren't loaded yet.") django.core.exceptions.appregistrynotready: models aren't loaded yet. 

but when change line "obj_state=type(item).objects.filter(crit=val)" "obj_state=type(item).objects.filter(statename=val)" works fine. there seems problem variable "crit" whicht stands searchcriteria, in example "statename". want pass crit via arguments method "import_so" reason doesn't work.

when print variable crit right before error occurs, content of variable seems correct.

any ideas?

update: after adding your_djangoproject_home="~/desktop/projects/myproject , os.environ['django_settings_module'] = 'myproject.settings'

to code following error:

traceback (most recent call last):   file "load_data.py", line 11, in <module>     django.setup()   file "/usr/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup     apps.populate(settings.installed_apps)   file "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate     app_config = appconfig.create(entry)   file "/usr/lib/python2.7/dist-packages/django/apps/config.py", line 112, in create     mod = import_module(mod_path)   file "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module     __import__(name) importerror: no module named myproject 

update: installed apps:

installed_apps = (     'django.contrib.admin',     'django.contrib.auth',     'django.contrib.contenttypes',     'django.contrib.sessions',     'django.contrib.messages',     'django.contrib.staticfiles',     'myproject.myapp' ) 

you need call django.setup() in script before use orm access data.

import django  sys.path.append(your_djangoproject_home) os.environ['django_settings_module'] = 'settings'  django.setup() 

see the docs more info.


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? -