2016-09-09 51 views
0

因为我有同样的抽象模型中的每一个应用程序的模型我必须避免冗余 我刚搬到我共同的抽象模型到新的应用程序,我创建并把它称为共同为什么我得到django.core.exceptions.AppRegistryNotReady:模型尚未加载?

常见Mo​​dels.py现在

from django.db import models 

LANGUAGE_CHOICES = (
    ('E','ENGLISH'), 
    ('F','FRENCH'), 
    ('S','SPANISH'), 
) 




class CommonInfo(models.Model): 
    created = models.DateTimeField("creation date", auto_now_add=True) 
    modified = models.DateTimeField("modification date", auto_now=True) 
    description = models.TextField() 

    class Meta: 
     abstract = True 

然后我说我的共同应用的settings.py

最后,在我所有的应用程序,我添加

from common.models import CommonInfo 
from common.models import LANGUAGE_CHOICES 

运行的本地服务器之后,这是我在终端得到错误(完整的跟踪)

回溯(最近通话最后一个):文件“manage.py”,10号线,在 execute_from_command_line(SYS .argv)文件 “C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django的\芯\米 anagement__init__.py”,线路338,在execute_from_command_line utility.execute()文件“C: \用户\鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django的\芯\米 anagement__init__.py “线312,在执行 django.setup()文件” C:\用户\鲍里斯\ dev的\出租\ virtrentout \ lib \ site-packages \ django__init .py“,第18行,设置为 apps.populate(settings.INSTALLED_APPS)文件”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ apps \ r egistry.py ”线108,在填入 app_config.import_models(all_models)文件 “C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django的\应用\ C onfig.py”,线路198,在import_models self.models_module = import_module(models_module_name)文件 “C:\ python27 \ LIB \ importlib__init py” 为37行,在import_module 进口(名称)文件“C:\用户\鲍里斯\ dev的\ rentout \租借\租赁\ models.py“,行94,在 class LeaseFilter(django_filters.FilterSet):File”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django_filter s \ filterset.py“,第181行,在 filters = new_class.filters_for_model(opts.model,opts)文件”C:\ Users \ Boris \ dev \ \ virtrentout \ lib \ site-packages \ django_filter s \ filterset.py“,行456,在filters_for_model cls.filter_for_reverse_field文件”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django_filter 小号\ filterset.py”,线路104,在filters_for_model filter_ = filter_for_field(场中,f)文件 “C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django_filter 小号\ filterset.py” ,line 461,in filter_for_field f,lookup_type = resolve_field(f,lookup_expr)文件“C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-pack年龄\ django_filter 小号\ utils.py “线路104,在resolve_field 查询= model_field.model._default_manager.all()查询文件。” C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django的\分贝\ MOD ELS \ manager.py”,线路228,在所有 返回self.get_queryset()文件 “C:\用户\鲍里斯\ dev的\ rentout \ rentout \租赁\ models.py”,第15行,在 get_q ueryset 回报超(NotTerminatedActiveManager,个体经营).get_queryset()过滤器(is_term inated =假IS_ACTIVE = TRUE)
文件 “C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ LIB \网站-packages \ django \ db \ mod els \ query。PY “线路679,在过滤器 回报self._filter_or_exclude(假,* ARGS,** kwargs)文件” C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ LIB \站点包\ Django的\ DB \国防部 els \ query.py“,第697行,在_filter_or_exclude clone.query.add_q(Q(* args,** kwargs))文件”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ db \ mod els \ sql \ query.py“,行1301,在add_q 子句中,require_inner = self._add_q(where_part,self.used_aliases)文件 ”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ db \ mod els \ sql \ query.py“,第1328行,在_add_q current_negated = current_negated,connector = connector,allow_joins = allow_join s)文件 ”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ db \ Mod els \ sql \ query.py“,第1144行,在build_filter中 lookups,parts,reffed_aggregate = self.solve_lookup_type(arg)文件 ”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ db \ mod els \ sql \ query.py“,第1030行,位于solve_lookup_type中 _,field,_,lookup_parts = self.names_to_path(lookup_splitted,self.get_met a())文件 ”C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ db \ mod els \ sql \ query.py“,行1383,在names_to_path中 field_names = list(get_field_names_from_opts(opts))文件”C:\ Users \鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django的\分贝\ MOD ELS \ SQL \ query.py “线43,在用于在opts.get_fields˚Fget_field_names_from_opts ()文件” C:\用户\鲍里斯\ dev的\租金out \ virtrentout \ lib \ site-packages \ django \ db \ mod els \ options.py“,第740行,在get_fields中 返回self._get_fields(include_parents = include_parents,include_hidden = incl ude_hidden)文件 ”C:\ Users \鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django的\分贝\ MOD ELS \ options.py “线802,在_get_fields all_fields = self._relation_tree文件” C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ LIB \站点包\ Django的\ utils的\ functional.py” 60行,在得到 解析度=实例。 字典 [self.name] = self.func(实例)文件 “C:\用户\鲍里斯\ dev的\ rentout \ virtrentout \ lib中\站点包\ django的\分贝\ MOD ELS \ options.py”,线709,在_relation_tree中 返回self._populate_directed_relation_graph()在_populate_directed_relation_graph中的第681行文件“C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ db \ mod els \ options.py” all_models = self.apps.get_models(include_auto_created = True)文件 “C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ utils \ lru_cache.py”,第101行,封装 result = user_function(* args,** kwds)文件“C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ apps \ r egistry.py”,第168行,在get_models self.check_models_ready()文件“C:\ Users \ Boris \ dev \ rentout \ virtrentout \ lib \ site-packages \ django \ apps \ r egistry.py”,in line 131,in check_models_ready raise AppRegistryNotReady(“Models aren 't尚未装入。“)django.core.exceptions.AppRegistryNotReady:模型尚未加载。

可能是什么问题呢?如果有更好的方法来分组所有常见的抽象模型和选择以避免随机数?

UPDATE:

这是错误的我忘了,包括

from django.db import models 
from concurrency.fields import IntegerVersionField 
from client.models import Tenant 
from unit.models import Unit, Extra 
from common.models import CommonInfo 
from common.models import NOTE_STATUS_CHOICES 
import django_filters 

# Create your models here. 



class NotTerminatedActiveManager(models.Manager): 
    def get_queryset(self): 
     return super(NotTerminatedActiveManager, self).get_queryset().filter(is_terminated=False,is_active=True) 


class Lease(CommonInfo): 
    version = IntegerVersionField() 
    amount = models.DecimalField(max_digits=7, decimal_places=2) 
    is_notrenewed = models.BooleanField(default=False) 
    unit = models.ForeignKey(Unit) 
    is_terminated = models.BooleanField(default=False) 
    not_terminated_active_objects = NotTerminatedActiveManager() 
    def __unicode__(self): 
     return u'%s %i %s %s ' % ("#", self.id,"unit", self.unit) 

class LeaseConditions(CommonInfo): 
    version = IntegerVersionField() 
    start_date = models.DateTimeField() 
    end_date = models.DateTimeField() 
    lease = models.ForeignKey(Lease) 
    increase = models.DecimalField(max_digits=7, decimal_places=2) 
    amount = models.DecimalField(max_digits=7, decimal_places=2) 
    is_terminated = models.BooleanField(default=False) 
    not_terminated_active_objects = NotTerminatedActiveManager() 
    def __unicode__(self): 
     return u'%s %i %s %s %s %s %s %s' % ("#", self.id,"first_name", self.first_name, "last_name", self.last_name, "phone", self.phone) 

    def clean(self): 
     model = self.__class__ 
     if self.lease_id and (self.is_terminated == False) and model.objects.filter(lease=self.lease, is_active=True).count() == 1: 
      raise ValidationError('!Lease has a active condition already, Terminate prior to creation of new one'.format(self.lease)) 

class LeaseTenant(CommonInfo): 
    version = IntegerVersionField() 
    tenant = models.ForeignKey(Tenant) 
    lease = models.ForeignKey(Lease) 
    is_financialy_accountable = models.BooleanField(default=True) 


class LeaseExtra(CommonInfo): 
    version = IntegerVersionField() 
    extra = models.ForeignKey(Extra) 
    lease = models.ForeignKey(Lease) 
    is_included = models.BooleanField(default=True) 





class LeaseDiscount(CommonInfo): 
    version = IntegerVersionField() 
    amount = models.DecimalField(max_digits=7, decimal_places=2) 
    leaseconditions = models.ForeignKey(LeaseConditions) 
    period_date = models.DateTimeField() 


class LeasePayment(CommonInfo): 
    version = IntegerVersionField() 
    amount = models.DecimalField(max_digits=7, decimal_places=2) 
    lease = models.ForeignKey(Lease) 
    period_payed_for = models.DateTimeField() 
    payment_date = models.DateTimeField() 



class Note(CommonInfo): 
    version = IntegerVersionField() 
    title = models.CharField(max_length=200) 
    contact = models.ForeignKey(Lease) 
    followup_date = models.DateTimeField(null=True, blank=True) 
    status = models.CharField(max_length=1, default='P', 
           choices=NOTE_STATUS_CHOICES) 
    #widgets = { 
      #Use localization and bootstrap 3 
      #'datetime': DateTimeWidget(attrs={'id':"id_followup_date"}, usel10n = True, bootstrap_version=3)} 
    def __unicode__(self): 
     return u'%s %s %s %s %s %i %s %s %s %s %s %s %s %s' % ("Status:", self.status, "Follow Date:", self.followup_date, "Note #:", self.id, "Published:", self.creation_time, "Author:",self.author,"/Title:" , self.title,"/Body:" , self.body ) 




class LeaseFilter(django_filters.FilterSet): 
    class Meta: 
     model = Lease 
     fields = ['is_notrenewed', 'unit', 
     'is_terminated'] 
     #provider = django_filters.ModelChoiceFilter(queryset=Provider.objects.all(), 
     #name = django_filters.ModelChoiceFilter(queryset=Lease.objects.all().order_by('name')) 
+0

您是否在应用程序目录中添加了__init__? – kt14

+0

“最后,我添加了所有我的应用程序:[进口]” 请问您能提供一个实际的例子吗? – vmonteco

+0

回溯显示错误出现在您未显示的'rentout \ lease \ models.py'中。 – Alasdair

回答

1

我不认为LeaseFilter类在你的模型所属的位置。它看起来好像是在加载类时试图执行查询。由于模型尚未加载,这会导致错误。尝试将课程移到其他地方,例如您的views.py

相关问题