简体   繁体   English

django抽象模型继承导入

[英]django abstract model inheritance import

I wonder how i can import an abstract model into another app 我不知道如何将抽象模型导入另一个应用程序

world_elements holds: world_elements持有:

class Location(models.Model):
    """
    Holds x,y coordinates of a virtual 2d map. 
    """

    x = models.IntegerField()
    y = models.IntegerField()

    class Meta:
        abstract = True

    def __unicode__(self):
        return "%s, %s" % (self.x, self.y)

now in another app i try: 现在在另一个应用程序中,我尝试:

from world_elements.models import Location

class NpcTown(Location):
    """
    A town with their coordinates trianinggrounds quest office and all other relevant attributes
    """

    # general town information
    name = models.CharField(max_length = 63)
    flavor = models.TextField(max_length = 511)
    guild = models.ForeignKey(NpcGuild)

    # locations
    trainingground = models.ForeignKey(TrainingGround, null=True)

    def __unicode__(self):
        return self.name

but now i get ImportError: cannot import name Location 但现在我得到ImportError:无法导入名称位置

How do i import an abstract model? 如何导入抽象模型?

Simplifying the names of the classes a bit, the following works for me in Django 1.7, which is the latest stable release at the time of this writing. 有点简化了类的名称,以下内容在Django 1.7中为我工作,这是在撰写本文时最新的稳定版本。

Directory layout 目录布局

    project
          \_ apps
              \_ __init__.py
              \_ A
              \_ B
          \_ config
              \_ __init__.py
              \_ settings.py
              \_ urls.py
              \_ wsgi.py
          \_ data
          \_ makefile
          \_ manage.py
          \_ README.md

In the above, app A contains the abstract model. 在上面,应用程序A包含抽象模型。 B uses it, as follows: B使用它,如下所示:

Abstract Class(es) 抽象类

class AModel(Model):
    ...
    class Meta:
        abstract = True

Then 然后

Concrete Class(es) 具体课程

from apps.A.models import AModel

class BModel(AModel):
    ...
    blah = "ayyo"

Note that apps, A, and B all must contain an __init__.py file. 请注意,应用程序A和B必须都包含__init__.py文件。

Don't be afraid to break free of the Django directory layout conventions imposed by manage.py start{app,project} . 不要害怕摆脱manage.py start{app,project}施加的Django目录布局约定。 Doing so will free your mind and you will love having things neatly organized. 这样做可以解放您的思想,并且您会喜欢将事情组织得井井有条。

Another thing that helps debugging module imports is simply print ing the module imported. 有助于调试模块导入的另一件事就是简单地print导入的模块。 Then you can tell what is actually being resolved. 然后,您可以知道实际要解决的问题。 For example: 例如:

from apps.A.models import AModel
print AModel # <class 'apps.A.models.AModel'>

And: 和:

import apps
print apps # <module 'apps' from '/home/g33k/gits/checkouts/my/project/apps/__init__.pyc'>

In a normal structure like this: 在这样的正常结构中:

my_project
  - /my_project
    - /settings.py
  - /app1
    - /models.py
      * class Model1...
  - /app2
    - /models.py
      * class Model2...

From app1/models.py this worked for me: 从app1 / models.py这为我工作:

from django.db import models
from my_project.app1.models import Model1

class Model2(Model1):
    ...

Using Django 11.1 使用Django 11.1

尝试

from world_elements import Location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM