Django: models aren't being recognized by sync

2019-03-29 06:07发布

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

2条回答
冷血范
2楼-- · 2019-03-29 06:48

That might be missing app_label: http://docs.djangoproject.com/en/dev/ref/models/options/#app-label. More also in syncdb ignores imported models it covers most of it. If what please add more details so that answer could be updated.

UPDATE: http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/ seems that models might be OK. Migrations might be the problem.

查看更多
Summer. ? 凉城
3楼-- · 2019-03-29 07:05

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.

查看更多
登录 后发表回答