Django:模型在被重构为单独的文件后,才被syncDB或南方识别 [英] Django: models aren't being recognized by syncDB or south after they have been refactored into separate files

查看:136
本文介绍了Django:模型在被重构为单独的文件后,才被syncDB或南方识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型模型文件与所有的类和东西,很难维护在一个文件中。所以我已经将它重构成一个模型文件夹, init .py,并且每个类别都有一个。



然后我做了

  manage_noDebug.py schemamigration picviewer --auto 
manage_noDebug.py schemamigration migrate picviewer
/ pre>

,并从数据库中删除了表,但没有按照我的想法添加model_ *表。现在可以拿起我的模型文件吗?

  manage_noDebug.py sql picviewer 

从上面输出是空的



我的文件夹的结构是: p>

picviewer
/ models /
init .py
Picture.py
paperType.py
...



其中一个文件类是:

  class cartItem(models.Model):
picture = models.ForeignKey('Picture',null = False)
paperType = models.ForeignKey('paperType',null = False)
printSize = models.ForeignKey('printSize',null = False)
quantity = models.IntegerField(default = 1,validators = [validators.MinValueValidator(1)])
price = models.DecimalField(decimal_places = 2,max_digits = 8)
dateCreated = models.DateTimeField(null = False)
sessionKey = models.ForeignKey(Session,to_field =session_key,null = False)
user = models.ForeignKey(User,null = True)

class Meta:
app_label ='picviewer'

设置安装的应用程序:

  INSTALLED_APPS =(
'django.contrib.auth ',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages'
'django.contrib.admin',
'picviewer',
'south'

我已经尝试从项目目录中删除/迁移/目录并运行syncdb:

  D:\〜Sasha\eclipse_workspace\zavalen> manage_noDebug.py syncdb 
同步...
找不到灯具。

同步:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> django.contrib.admin
> picviewer
>南

未同步(使用迁移):
-
(使用./manage.py迁移迁移这些)

看起来像nativesyncDB或south schemamigration都看不到我的模型。



这里是表输出从dbShell:

  D:\〜Sasha\eclipse_workspace\zavalen> manage_noDebug.py dbshel​​l 
SQLite版本3.7.5
为说明输入.help
输入以;结尾的SQL语句
sqlite> .tables
auth_group auth_user_user_permissions
auth_group_permissions django_admin_log
auth_message django_content_type
auth_permission django_session
auth_user django_site
auth_user_groups south_migrationhistory
sqlite>

这是我的模型文件夹的目录:

  D:\〜Sasha\eclipse_workspace\zavalen\picviewer\models> dir * .py 
D的目录:\〜Sasha \eclipse_workspace\ zavalen\picviewer\models

15.04.2011 16:38 1 125 cartItem.py
15.04.2011 16:43 1 283 Collection.py
15.04.2011 16: 40 419 ImageSizeRatio.py
15.04.2011 16:43 876 Menu.py
15.04.2011 16:43 1 667 Order.py
15.04.2011 14:07 1 457 OrderForm.py
15.04.2011 16:43 490 OrderStatusHistory.py
15.04.2011 16:43 683 paperType.py
15.04.2011 16:43 3 202 Picture.py
15.04.2011 16 :43 1 520 printSize.py
15.04.2011 16:43 687 PurchaseItem.py
15.04.2011 16:43 1 239 Tools.py
15.04.2011 16:11 0 __init__.py


解决方案

要公开类,您必须在 __ init __。py ,如:

 从图片导入Picture 
from paperType import paperType
...
__all__ = ['Picture','paperType',...]

导入的顺序很重要。



如果不这样做,您没有访问路径 picviewer.models.Picture ,它是 picviewer.models.Picture.Picture


I had a large models file with all the classes and stuff and it was hard to maintain all in one file. So I've refactored that into a model folder, init.py and files one per each class.

then I did

manage_noDebug.py schemamigration picviewer --auto
manage_noDebug.py schemamigration migrate picviewer

and south removed the tables from the database but hasn't added the model_* tables as I thought it would. Can I get it to pick up my model files now?

manage_noDebug.py sql picviewer

output from the above is empty

the structure of my folders is:

picviewer /models/ init.py Picture.py paperType.py ...

one of the files classes is:

class cartItem(models.Model):
    picture = models.ForeignKey('Picture', null=False)
    paperType = models.ForeignKey('paperType', null=False)
    printSize = models.ForeignKey('printSize', null=False)
    quantity = models.IntegerField(default=1, validators=[validators.MinValueValidator(1)])
    price = models.DecimalField(decimal_places=2,max_digits=8)
    dateCreated = models.DateTimeField(null=False)
    sessionKey = models.ForeignKey(Session, to_field="session_key", null=False)
    user = models.ForeignKey(User,null=True)

    class Meta:
        app_label = 'picviewer'

settings installed apps:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'picviewer',
    'south'
)

I've tried removing /migrations/ directory from the project directory and running syncdb:

D:\~Sasha\eclipse_workspace\zavalen>manage_noDebug.py syncdb
Syncing...
No fixtures found.

Synced:
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.sites
 > django.contrib.messages
 > django.contrib.admin
 > picviewer
 > south

Not synced (use migrations):
 -
(use ./manage.py migrate to migrate these)

Looks like neither "native" syncDB or south schemamigration are seeing my models.

Here is tables output from dbShell:

D:\~Sasha\eclipse_workspace\zavalen>manage_noDebug.py dbshell
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
auth_group                  auth_user_user_permissions
auth_group_permissions      django_admin_log
auth_message                django_content_type
auth_permission             django_session
auth_user                   django_site
auth_user_groups            south_migrationhistory
sqlite>

Here's dir for my models folder:

D:\~Sasha\eclipse_workspace\zavalen\picviewer\models>dir *.py
Directory of D:\~Sasha\eclipse_workspace\zavalen\picviewer\models

15.04.2011  16:38             1 125 cartItem.py
15.04.2011  16:43             1 283 Collection.py
15.04.2011  16:40               419 ImageSizeRatio.py
15.04.2011  16:43               876 Menu.py
15.04.2011  16:43             1 667 Order.py
15.04.2011  14:07             1 457 OrderForm.py
15.04.2011  16:43               490 OrderStatusHistory.py
15.04.2011  16:43               683 paperType.py
15.04.2011  16:43             3 202 Picture.py
15.04.2011  16:43             1 520 printSize.py
15.04.2011  16:43               687 PurchaseItem.py
15.04.2011  16:43             1 239 Tools.py
15.04.2011  16:11                 0 __init__.py

解决方案

To expose classes you have to import them in the __init__.py, like:

from Picture import Picture 
from paperType import paperType 
...
__all__ = ['Picture', 'paperType', ...]

The order of importing is important.

If you don't do this, you have no access path picviewer.models.Picture, it's picviewer.models.Picture.Picture.

这篇关于Django:模型在被重构为单独的文件后,才被syncDB或南方识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆