I am using this code to populate my database:
JavaScript
x
59
59
1
import os
2
def populate():
3
python_cat = add_cat('Python')
4
5
add_page(cat=python_cat,
6
title="Official Python Tutorial",
7
url="http://docs.python.org/2/tutorial/")
8
9
add_page(cat=python_cat,
10
title="How to Think like a Computer Scientist",
11
url="http://www.greenteapress.com/thinkpython/")
12
13
add_page(cat=python_cat,
14
title="Learn Python in 10 minutes",
15
url="http://www.korokithakis.net/tutorials/python/")
16
17
django_cat = add_cat(name="Django")
18
19
add_page(cat=django_cat,
20
title="Official Django Tutorial",
21
url="http://djangoproject.com/en/1.5/intro/tutorial01/")
22
23
add_page(cat=django_cat,
24
title="Django Rocks",
25
url="http://www.djangorocks.com/")
26
27
add_page(cat=django_cat,
28
title="How to Tango with Django",
29
url="htttp://www.tangowithdjango.com/")
30
31
frame_cat = add_cat(name="Other Frameworks")
32
33
add_page(cat=frame_cat,
34
title="Bottle",
35
url="http://bottlepy.org/docs/dev/")
36
37
add_page(cat=frame_cat,
38
title="Flask",
39
url="http://flask.pocoo.org")
40
41
# Print out what we have added to the user.
42
for c in Category.objects.all():
43
for p in Page.objects.filter(category=c):
44
print "- {0} - {1}".format(str(c), str(p))
45
46
def add_page(cat, title, url, views=0):
47
p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
48
return p
49
50
def add_cat(name):
51
c = Category.objects.get_or_create(name=name)
52
return c
53
54
if __name__ == '__main__':
55
print "Starting Rango population script..."
56
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'p.settings')
57
from rango.models import Category, Page
58
populate()
59
On running
JavaScript
1
2
1
python c:python27ppopulate_rango.py
2
It gives the error:
JavaScript
1
61
61
1
Staring Rango population script
2
Traceback (most recent call last):
3
File "c:python27ppopulate_rango.py", line 59, in <module>
4
populate()
5
File "c:python27ppopulate_rango.py", line 4, in populate
6
python_cat = add_cat('Python')
7
File "c:python27ppopulate_rango.py", line 52, in add_cat
8
c = Category.objects.get_or_create(name=name)
9
File "C:Python27Libsite-packagesdjangodbmodelsmanager.py", li
10
manager_method
11
return getattr(self.get_queryset(), name)(*args, **kwargs)
12
File "C:Python27Libsite-packagesdjangodbmodelsquery.py", line
13
et_or_create
14
return self.get(**lookup), False
15
File "C:Python27Libsite-packagesdjangodbmodelsquery.py", line
16
clone = self.filter(*args, **kwargs)
17
File "C:Python27Libsite-packagesdjangodbmodelsquery.py", line
18
ilter
19
return self._filter_or_exclude(False, *args, **kwargs)
20
File "C:Python27Libsite-packagesdjangodbmodelsquery.py", line
21
filter_or_exclude
22
clone.query.add_q(Q(*args, **kwargs))
23
File "C:Python27Libsite-packagesdjangodbmodelssqlquery.py",
24
in add_q
25
clause, require_inner = self._add_q(where_part, self.used_aliases)
26
File "C:Python27Libsite-packagesdjangodbmodelssqlquery.py",
27
in _add_q
28
current_negated=current_negated, connector=connector)
29
File "C:Python27Libsite-packagesdjangodbmodelssqlquery.py",
30
in build_filter
31
lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
32
File "C:Python27Libsite-packagesdjangodbmodelssqlquery.py",
33
in solve_lookup_type
34
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, se
35
a())
36
File "C:Python27Libsite-packagesdjangodbmodelssqlquery.py",
37
in names_to_path
38
field, model, direct, m2m = opts.get_field_by_name(name)
39
File "C:Python27Libsite-packagesdjangodbmodelsoptions.py", li
40
get_field_by_name
41
cache = self.init_name_map()
42
File "C:Python27Libsite-packagesdjangodbmodelsoptions.py", li
43
init_name_map
44
for f, model in self.get_all_related_m2m_objects_with_model():
45
File "C:Python27Libsite-packagesdjangodbmodelsoptions.py", li
46
get_all_related_m2m_objects_with_model
47
cache = self._fill_related_many_to_many_cache()
48
File "C:Python27Libsite-packagesdjangodbmodelsoptions.py", li
49
_fill_related_many_to_many_cache
50
for klass in self.apps.get_models():
51
File "C:Python27Libsite-packagesdjangoutilslru_cache.py", line
52
rapper
53
result = user_function(*args, **kwds)
54
File "C:Python27Libsite-packagesdjangoappsregistry.py", line 1
55
_models
56
*self.check_models_ready()
57
File "C:Python27Libsite-packagesdjangoappsregistry.py", line 1
58
ck_models_ready
59
raise AppRegistryNotReady("Models aren't loaded yet.")
60
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.*
61
Rest of my files are ok but getting this error. I am following the tutorial from Tango with Django book but as the book refers to Django 1.5.4 and i am using Django 1.8, so can anyone help me here?
Advertisement
Answer
I had the same exception with Django 1.7rc2. The solution was to add these lines at the beginning of my program:
JavaScript
1
3
1
import django
2
django.setup()
3