开发者

How Django skipping an app when using syncdb command

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/techtr开发者_C百科ee/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.


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.


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 *


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.


Check the file name is

__init__.py

and not

init.py

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜