Python 模型尚未安装或抽象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21044885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:53:39  来源:igfitidea点击:

Model has either not been installed or is abstract

pythondjango-modelsforeign-keys

提问by sina

When I try to migrate my code I get this error. Here are my code and classes:

当我尝试迁移我的代码时,出现此错误。这是我的代码和类:

from django.db import models
from core.models import Event

class TicketType(models.Model):
    name = models.CharField(max_length=45)
    price = models.DecimalField(max_length=2, decimal_places=2, max_digits=2)
    type = models.CharField(max_length=45)
    amount = models.IntegerField()
    event = models.ForeignKey(Event)

class Meta:
    app_label = "core"


import datetime
from django.core.serializers import json
from django.db import models
from core.models import User


class Event(models.Model):
    page_attribute = models.TextField()
    name = models.TextField(max_length=128 , default="New Event")
    description = models.TextField(default="")
    type = models.TextField(max_length=16)
    age_limit = models.IntegerField(default=0)
    end_date = models.DateTimeField(default=datetime.datetime.now())
    start_date = models.DateTimeField(default=datetime.datetime.now())
    is_active = models.BooleanField(default=False)
    user = models.ForeignKey(User)
    ticket_type=models.ForeignKey('core.models.ticket_type.TicketType')

    class Meta:
            app_label = "core"

Here is the error I get:

这是我得到的错误:

CommandError: One or more models did not validate: core.event: 'ticket_type' has a relation with model core.models.ticket_type.TicketType, which has either not been installed or is abstract.

CommandError:一个或多个模型未验证:core.event:'ticket_type'与模型core.models.ticket_type.TicketType有关系,该模型尚未安装或抽象。

采纳答案by Daniel Roseman

You're unnecessarily confusing yourself by having these in separate files within the same app.

将这些放在同一个应用程序中的单独文件中,您会不必要地混淆自己。

But your issue is caused by the way you're referenced the target model. You don't use the full module path to the model: you just use 'app_name.ModelName'. So in your case it should be:

但是您的问题是由您引用目标模型的方式引起的。您不使用模型的完整模块路径:您只需使用'app_name.ModelName'. 所以在你的情况下它应该是:

ticket_type=models.ForeignKey('core.TicketType')

回答by andilabs

Another issue can be when using multiple models in separate files missing statement like:

另一个问题可能是在单独的文件中使用多个模型时缺少语句,例如:

class Meta:
    app_label = 'core_backend'

回答by Mark Chackerian

You can also get this error if there a bug in your models file that prevents it from loading properly. For example, in models.py

如果您的模型文件中存在阻止其正确加载的错误,您也可能会收到此错误。例如,在models.py

from third_party_module_i_havent_installed import some_method

回答by yndolok

I hit this error when I didn't put a third-party app in my INSTALLED_APPSsetting yet.

当我还没有在我的INSTALLED_APPS设置中放置第三方应用程序时,我遇到了这个错误。