本文介绍了Django - manage.py sql APPNAME不生成模型SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对较大的平面应用程序,我正在努力。为了保持关注的分离,我已经将模型拆分为 auth_models dashboard_models taxonomy_models 等等。这些已被放置在文件夹结构中

  APPNAME / 
app /
models /
__init__.py
auth_models.py
dashboard_models.py
taxonomy_models.py
...
视图/
__init__.py
dashboard_views .py
taxonomy_views.py
...

我的 app / models / __ init __。py 具有以下内容:

 从auth_models import * 
from dashboard_models import *
from taxonomy_models import *

但是,当我运行 ./ manage.py sql app ,我没有任何输出。没有警告,根本没有。



这个问题的原因是我正在实施一个数据库修改和迁移,其中South无法处理几个方面。所以我基本上重新开始一个新的模式,并且稍后会实现一个数据转换脚本来迁移现有的数据集。为此,我需要模式来创建模型表。

解决方案

请参阅的答案。



Django从模型所在的路径中做出关于你的应用程序名称的假设,所以你在这种情况下,强制在每个导入的模型中添加一个应用程序标签,如下所示:

  class MyModel(Model):
#模型字段
class Meta:
app_label ='app'

背景



在撰写本文时,Django具有以下代码来检测模型的应用标签:

$ b $如果getattr(meta,'app_label',None)为None,则
#通过查找一个级别来绘制出app_label。
#对于'django.contrib.sites.models',这将是'sites'。
model_module = sys.modules [new_class .__ module__]
kwargs = {app_label:model_module .__ name __。split('。')[ - 2]}

从这里,我们看到它从可能存在的模块名称推断出 app_label 深深的应用层次结构。


I have a relatively large flat application that I'm working on. To maintain separation of concerns, I've split the model and view files into auth_models, dashboard_models, taxonomy_models and more. These have been placed in the folder structure as

APPNAME/
  app/
    models/ 
      __init__.py
      auth_models.py
      dashboard_models.py
      taxonomy_models.py
      ...
    views/
      __init__.py
      dashboard_views.py
      taxonomy_views.py
      ...

My app/models/__init__.py has the following:

from auth_models import *
from dashboard_models import *
from taxonomy_models import *

However, when I run ./manage.py sql app, I get no output whatsoever. No warnings, nothing at all.

The reason for this question is I'm implementing a database modification and migration in which South cannot handle several aspects. So I'm essentially starting over with a fresh schema and will later implement a data conversion script to migrate the existing dataset. To do this, I need the schema to create the model tables.

解决方案

See this answer.

Django makes assumptions about your app name from the path in which the models live, so you're forced in this situation to add an app label to every imported model like this:

class MyModel(Model):
    # Model fields
    class Meta:
        app_label = 'app'

Background:

As of this writing, Django has the following code to detect an app label for a model:

    if getattr(meta, 'app_label', None) is None:
        # Figure out the app_label by looking one level up.
        # For 'django.contrib.sites.models', this would be 'sites'.
        model_module = sys.modules[new_class.__module__]
        kwargs = {"app_label": model_module.__name__.split('.')[-2]}

From this, we see that it infers the app_label from the modules name, which may exist deep in the app hierarchy.

这篇关于Django - manage.py sql APPNAME不生成模型SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:10