2

I have a Django project which has two apps (one created as debug test). For the debug test, syncdb does put the model in the database but for the other it does not.

  • Both are in settings.INSTALLED_APPS.
  • There are around seven models, none of them being recognized.
  • Neither the server, any page or the syncdb-console give any errors.
  • Models are in a models directory. As a test, there is also one in app/models.py (doesn't work either).
  • Most strikingly to me is that the below code does display the models which aren't synced (executed from the app that is skipped):
for model in get_models():  
    models.append(model)  
pass models to a template  

Any help would be much appreciated. I think it is something trivial but I'm out of ideas for things to try.

Thanks,

UPDATE:

INSTALLED_APPS = (  
    'django.contrib.auth',  
    'django.contrib.contenttypes',  
    'django.contrib.sessions',  
    'django.contrib.messages',  
    'django.contrib.admin',  
    'techtree',  
    'froink',  
)

Structure:

  • project/techtree/models.py (contains a test model)
  • project/techtree/models/__init__.py (as described here)
  • project/techtree/models/typ.py (contains model Type)

There are more files of the same type as the last line.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Mark
  • 18,730
  • 7
  • 107
  • 130

4 Answers4

2

Are you missing the __init__.py file in the second app's models directory? That would mean it can't be found on the path.

Can you show us your INSTALLED_APPS setting, and your directory structure please?

Looking at your directory structure, I think I can guess what's wrong.

my_app/
    __init__.py
    my_module.py
    my_module/
        __init__.py
        my_python_file.py

With the above fictional directory structure, what gets imported when I do the following?

from my_module import *

Does that import everything from within my_module.py or everything within the my_module directory?

Use one, or the other. Not both. Put everything inside your models.py file, and get rid of the directory unless you have a good reason for having a models directory. You probably don't.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • Thanks for the answer, I added the requested info – Mark Apr 25 '11 at 00:54
  • Fck I'm stupid, should have thought of that... Thanks for the answer the test model in models.py is being recognized now! I'll see if I can get the directory to work (with models.py removed) and otherwise stick with this. – Mark Apr 25 '11 at 01:05
  • @Mark, putting all your models in one file in python is fairly natural. If you must separate them out into separate files, be sure to import all the externally accessible models in the `models/__init__.py` file. GL – Josh Smeaton Apr 25 '11 at 01:08
2

I faced this same problem, It took hours for me to figure out how to group models in to a separate directory. Heres how to do it,

Add a meta class to each of your models with an app_label parameter.

from django.db import models

class Test(models.Model):
    class Meta:
        app_label = 'myapp'

Here's where I found out about this, Placing Django Models In Separate Files

But doing this wasn't enough you have to import the models in an __init__.py file in the models directory like this,

from YourModelFile import *
vim
  • 1,098
  • 9
  • 21
0

Django is usually searching for a models.py file only. If you have models.py and a module sub directory called models with an __init__.py file it is not recognizing the models correctly (my guess). You can do one of these two:

  • Remove your sub-module completely and keep all your model definitions in models.py

OR

  • Remove models.py in techtree. Add a models.py file to your techtree.models app where you keep the model definitions. Add 'techtree.models' to your INSTALLED_APPS (just 'techtree' is no enough).

Hope one of these answers helps.

Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48
  • Adding the the models directory to the installed apps will not work. INSTALLED_APPS is used for things like finding templates and static content as well - not just model definitions. – Josh Smeaton Apr 25 '11 at 01:35
  • Adding means adding. Not removing the ``'techtree'`` and add ``'techtree.models'`` instead. Add both and templatetags and static files are available. – Torsten Engelbrecht Apr 25 '11 at 02:03
0

Check the file name is

__init__.py

and not

init.py

Timbadu
  • 1,533
  • 1
  • 10
  • 16
  • 1
    Yeah that is a bit confusing, I typed __ init__ but I'm not very familiar with the language here so it made it bold instead of just displaying what I typed; sorry about that. – Mark Apr 25 '11 at 18:34