Django不为模型创建数据库表(不包括syncdb和南) [英] Django not creating db tables for models (neither with syncdb nor South)

查看:134
本文介绍了Django不为模型创建数据库表(不包括syncdb和南)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Centos VPS上有一个Django项目。



我创建了一些模型并对它们进行了调试,以便它们验证并且不会发生任何错误。
我将它们放在myapp中的models文件夹中,并将每个模型添加到此目录中的init文件中,例如:



从类别导入类别



我将应用添加到settings.py INSTALLED_APPS 并运行:

  Python manage.py syncdb 

它似乎工作正常,并添加了所有表与我的应用程序。



然后我安装了南方,并将其添加到 INSTALLED_APPS 并再次尝试syncdb并运行:

  Python manage.py schemamigration myapp --initial 

它正确生成文件,但没有任何内容(我的模型没有表)。



模型文件夹(usertype.py)中的示例文件

 从django.db导入模型

class UserType(models.Model):
usertype_id = models.PositiveIntegerField(primary_key = True)
description = model s.CharField(max_length = 100)
is_admin = models.BooleanField()
is_moderator = models.BooleanField()

class Meta:
app_label ='myapp'

任何想法在这里可能会出现什么,为什么我无法得到任何东西来检测我的模型?

解决方案

你误解了与南方合作的过程。南方不仅仅是另一个应用,它是一个管理工具。您的应用程序需要从开始或转换为一个南方应用程序。话虽如此,这个过程是这样的:


  1. 将南加到INSTALLED_APPS

  2. 运行syncdb第一次

  3. 将您的应用添加到INSTALLED_APPS *

  4. 运行南初始化命令:

      python manage.py schemamigration myapp --initial 


  5. 迁移:

      python manage.py migrate 


如果要转换项目:


  1. 在添加南方之后运行syncdb

  2. 运行:



    管理.py convert_to_south myapp


从现在开始,使用南方来管理您的迁移。 / p>

* ps - 您可以同时添加南方和您自己的应用程序,如果您记住将自己的应用程序放在南部。这是因为django按顺序读取INSTALLED_APPS - 它在所有应用程序上运行syncdb,但在安装南方之后,它不会安装其余的,而是告诉您使用南方命令来处理这些



编辑



我误导了你由于你非常重视南方的事情,我没有意识到,问题是您正在尝试使用模型作为目录模块而不是普通文件。这是在django中认出的问题,解决方法实际上与您一样,尽管如此:



说这是你的结构:

  project / 
myapp /
models /
__init__.py
bar.py

您需要 bar.py 如下所示:

 从django.db导入模型

class Foo(models.Model):
#fields ...

class Meta:
app_label ='myapp ' #你需要这个!

__ init __。py 需要看起来像这是:

 从酒吧进口Foo 

确保它看起来像这样,它会工作。



更新18/08/2014 p>

票据已更改为 wontfix ,因为显然,app_label 已修复更大的问题。 Huzza!


I have a Django project on a Centos VPS.

I created some models and debugged them so they validate and give no errors. I have them in a "models" folder in my myapp and have added each model to the init file in this directory, for example:

from category import Category

I added the app to settings.py INSTALLED_APPS and ran:

Python manage.py syncdb

It appeared to work fine and added all tables apart from the ones for my app.

I then installed South and added that to INSTALLED_APPS and, tried syncdb again and ran:

Python manage.py schemamigration myapp --initial

It generated the file correctly but nothing was in it (none of the tables my models).

An example file in "models" folder (usertype.py)

from django.db import models

class UserType(models.Model):
    usertype_id = models.PositiveIntegerField(primary_key=True)
    description = models.CharField(max_length=100)
    is_admin = models.BooleanField()
    is_moderator = models.BooleanField()

class Meta:
    app_label = 'myapp'

Any ideas what could be going wrong here and why I can't get anything to detect my models?

解决方案

you're misunderstanding the process of working with south. South isn't just another application, it's a managing tool. Your app needs to be a South application from the begining or converted to one. That being said, the process is like so:

  1. Add South to INSTALLED_APPS
  2. run syncdb for the first time
  3. Add your app to INSTALLED_APPS*
  4. run the south initialization command:

    python manage.py schemamigration myapp --initial
    

  5. migrate:

    python manage.py migrate
    

If you want to convert a project:

  1. Run syncdb after adding south
  2. run:

    manage.py convert_to_south myapp

And use south from now on to manage your migrations.

*p.s. - you can add both south and your own app at the same time, if you keep in mind to put south before your own apps. That's because django reads INSTALLED_APPS in order - it runs syncdb on all apps, but after installing south it won't install the rest and instead tell you to use the south commands to handle those

edit

I misled you. Since you put so much emphasis on the south thing I didn't realize the problem was you were trying to use models as a directory module instead of a normal file. This is a recognized problem in django, and the workaround is actually exactly as you though in the first place:

say this is your structure:

project/
       myapp/
            models/
                  __init__.py
                  bar.py

you need bar.py to look like this:

from django.db import models

class Foo(models.Model):
    # fields...

    class Meta:
        app_label = 'myapp' #you need this!

and __init__.py needs to look like this:

from bar import Foo

Make sure it looks like this and it will work.

UPDATE 18/08/2014

The ticket has changed to wontfix, because apparently the bigger issue with the app_label has been fixed. Huzza!

这篇关于Django不为模型创建数据库表(不包括syncdb和南)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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