0

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

6
  • I believe the app label needs needs to be unique per installed app. Have you tried just having them be app1, app2, etc, rather than app for everything? Commented Dec 15, 2013 at 20:24
  • Just tried that but unfortunately it makes no difference... Thanks for the suggestion. Commented Dec 15, 2013 at 20:54
  • It's not very clear what is [app] used in Meta? Is it added to installed apps? Does it conatin model.py file, possible empty? can you try to add empty model.py to core folder to make it django application, and add it to installed apps? Commented Dec 15, 2013 at 21:18
  • Thanks, that worked! However I do not understand why. For example by adding django.contrib.admin one gets the admin tables for admin log created without the need for an empty models.py or adding an "empty" app to the installed apps... confusing. Add your comment as an answer and I will give you the credits. Commented Dec 15, 2013 at 21:47
  • app labels need to be unique; what is the problem you are trying to solve here? Commented Dec 16, 2013 at 7:13

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.