Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

django - Model in sub-directory via app_label?

In order to place my models in sub-folders I tried to use the app_label Meta field as described here.

My directory structure looks like this:

  • project
    • apps
      • foo
        • models
          • __init__.py
          • bar_model.py

In bar_model.py I define my Model like this:

from django.db import models

class SomeModel(models.Model):

    field = models.TextField()

    class Meta:
        app_label = "foo"

I can successfully import the model like so:

from apps.foo.models.bar_model import SomeModel

However, running:

./manage.py syncdb

does not create the table for the model. In verbose mode I do see, however, that the app "foo" is properly recognized (it's in INSTALLED_APPS in settings.py). Moving the model to models.py under foo does work.

Is there some specific convention not documented with app_label or with the whole mechanism that prevents this model structure from being recognized by syncdb?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See Django ticket #10985: Explain how models can be organised in a directory

It may be that you aren't importing your models into __init__.py?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...