Roger
Roger

Reputation: 4797

Struggling with Django syncdb

This drives me mad... I'm reorganizing an existing Django project using the following structure:

[project_abc]
  [app]
    [core]
      [app1]
        admin.py
        models.py
        ...
      [app2]
        admin.py
        models.py
        ...
      ... etc ... there's a total of 9 apps
    [rest]
      ... rest api stuff, non-db related ...
    [mobile]
      ... mobile stuff, non-db related ...
    [
  south
  tastypie
  [project_abc]
    settings.py
    urls.py
  manage.py

All apps with models that require database access have been added to settings.py:

INSTALLED_APPS = (
  'django.contrib.admin',
  '[app].[core].[app1]',
  '[app].[core].[app2]',
  ...
  'tastypie',
  'south'
)

Each model class has a Meta class like:

class Meta:
  app_label=[app] # this points to the top level above [core]

Directories [app], [core] and subsequent [app] directories have an __init__.py file in them.

When I run syncdb, it happily ignores my apps under [core] however the tables for other apps like tastypie and south get created properly.

When I run manage.py validate it returns 0 errors found

I've read probably all posts and hints on topics related to syncdb but unfortunately to no avail. I'm obviously missing something, but cannot figure out what it is....

Upvotes: 0

Views: 131

Answers (1)

alko
alko

Reputation: 48317

I can't fully understand which [app] is set in models Meta, but note that django syncdb uses django.db.models.get_apps to find projects' applications. Latter interspects apps from INSTALLED_APPS, and explicetely tries to load apps' models module with

models = import_module('.models', app_name)

So applications outside INSTALLED_APPS won't have tables synced.

Second, django loads all the models with django.db.models.get_apps for each found application, and latter turn introspects AppCache.apps_models (that cache is, as far as I remember, populated with register_models by model constructor). So all the imported models corresponding to valid applications are processed.

I guess you have to ensure that [app] from models._Meta:

  • contains models.py (possibly empty) which will make it a django application;
  • is mentioned in INSTALLED_APPS, to be asseccible with get_apps function.

Upvotes: 1

Related Questions