开发者

syncdb doesnt create tables for my app

开发者 https://www.devze.com 2023-02-16 07:03 出处:网络
Hello I am trying out the django framework, and my project structure is like this ../mysite/src/mysite/models/models.py (models.py containts all defined models)

Hello I am trying out the django framework, and my project structure is like this

../mysite/src/mysite/models/models.py (models.py containts all defined models) ../mysite/src/mysite/ (contains the settings.py manage.py and etc)

when i run the syncdb. only django admin's tables are created. but my models in models.py are not detected.

In INSTALLED_APPS section in settings.py. i added 'mysite'.

if i change from 'mysite' ->'mysite.src' it will give an error开发者_高级运维 "Error: no model named src" if i change from 'mysite' -> 'mysite.models.models' it will give 'AttributeError: 'module' object has no attribute 'path' '

can anyone help me with the syncdb?

thanks alot


Move your models.py file out from models dir and back into the mysite dir. Try again afterwards, it should now work... syncdb is reliant on the file/dir structure.


if you want to have a "models" folder containing many model definition files. You should include a file named init.py in the models folder where you should import all the models of the application

# app/models/__init__.py
# assuming you have main_models and other_models files 
from app.models.main_models import *
from app.models.other_models import *


Regardless of where your actual project directory is, you need to add the models app to your INSTALLED_APPS, in python dot syntax however your models app is resolvable.

If the top level mysite is in your python path, then it would be src.mysite.models. If src is in your python path, then mysite.models.

You are supposed to add the parent folder of your django project directory (where settings.py lives) to your python path, so your apps would be referred to as project.app


i found where the issue is. my Models.py should be models.py.... should not start with captical M.

thanks guys for your help


Your ideal app structure is

ProjectDirectory/
 |- settings.py
 |- urls.py
 \- AppDirectory
     |- __init__.py
     |- views.py
     \- models.py

If you want it to work in it's current configuration (I'm assuming /src is your "project directory" in this sense and has either been added to your python path or contains manage.py) then inside the models directoy include a file called __init__.py and add the line from models import *. This will import everything and make everything in your models.py accessible by django in the place it expects it to be.


add to your setting file

import os
import sys

ROOT_PATH = os.path.dirname(__file__)
sys.path.append(ROOT_PATH + '/src')

and then check it again

0

精彩评论

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