2016-09-22 49 views
0

阅读StackOverflow和互联网上的所有主题 - 没有运气。如何根据模型做出选择? - 模型尚未加载。 FK问题(似乎是)

# admindivisions.models 
class Countries(models.Model): 
    osm_id = models.IntegerField(db_index=True, null=True) 
    status = models.IntegerField() 
    population = models.IntegerField(null=True) 

    iso3166_1 = models.CharField(max_length=2, blank=True) 
    iso3166_1_a2 = models.CharField(max_length=2, blank=True) 
    iso3166_1_a3 = models.CharField(max_length=3, blank=True) 

    class Meta: 
     db_table = 'admindivisions_countries' 
     verbose_name = 'Country' 
     verbose_name_plural = 'Countries' 

class CountriesTranslations(models.Model): 
    common_name = models.CharField(max_length=81, blank=True, db_index=True) 
    formal_name = models.CharField(max_length=100, blank=True) 

    country = models.ForeignKey(Countries, on_delete=models.CASCADE, verbose_name='Details of Country') 
    lang_group = models.ForeignKey(LanguagesGroups, on_delete=models.CASCADE, verbose_name='Language of Country', 
            null=True) 

    class Meta: 
     db_table = 'admindivisions_countries_translations' 
     verbose_name = 'Country Translation' 
     verbose_name_plural = 'Countries Translations' 

# profiles.models 
from admindivisions.models import CountriesTranslations, Countries 

class AbstractProfile(models.Model): 
    COUNTRY_CHOICES =() 

    if (Languages.objects.model._meta.db_table in connection.introspection.table_names()): 
     # Just for test - This executes without any errors 
     CountriesTranslations.objects.filter(common_name="USA") 

     for country in Countries.objects.filter(status=1).exclude(iso3166_1='', iso3166_1_a2=''): 
      # Just for test - Also executes ok 
      CountriesTranslations.objects.get(common_name="USA") 

      # Makes problem (seems to be because of FK)     
      country_name = CountriesTranslations.objects.get(country=country) 
      COUNTRY_CHOICES += ((country.id, country_name),) 

    country = models.ForeignKey(Countries, verbose_name=_('country'), choices=COUNTRY_CHOICES, blank=True) 
    title = models.CharField(_('title'), max_length=30) 
    info = models.TextField(_('information'), max_length=500, blank=True) 

    class Meta: 
     abstract = True 

回溯:

Traceback (most recent call last): 
    File "./manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line 
    utility.execute() 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute 
    django.setup() 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/__init__.py", line 27, in setup 
    apps.populate(settings.INSTALLED_APPS) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate 
    app_config.import_models(all_models) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models 
    self.models_module = import_module(models_module_name) 
    File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 665, in exec_module 
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "/home/antonio/www/sportland/sportland/src/profiles/models.py", line 76, in <module> 
    class AbstractProfile(models.Model): 
    File "/home/antonio/www/sportland/sportland/src/profiles/models.py", line 109, in AbstractProfile 
    country_name = CountriesTranslations.objects.get(country=country) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/query.py", line 376, in get 
    clone = self.filter(*args, **kwargs) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/query.py", line 796, in filter 
    return self._filter_or_exclude(False, *args, **kwargs) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/query.py", line 814, in _filter_or_exclude 
    clone.query.add_q(Q(*args, **kwargs)) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1227, in add_q 
    clause, _ = self._add_q(q_object, self.used_aliases) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1253, in _add_q 
    allow_joins=allow_joins, split_subq=split_subq, 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1133, in build_filter 
    lookups, parts, reffed_expression = self.solve_lookup_type(arg) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1019, in solve_lookup_type 
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta()) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1308, in names_to_path 
    if field.is_relation and not field.related_model: 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/db/models/fields/related.py", line 111, in related_model 
    apps.check_models_ready() 
    File "/home/antonio/www/sportland/lib/python3.5/site-packages/django/apps/registry.py", line 131, in check_models_ready 
    raise AppRegistryNotReady("Models aren't loaded yet.") 
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. 

出现这种情况,如果我想执行任何manage.py指挥。

来自上面的几个测试我可以猜到问题是由于ForeignKey。如何解决它?

Django 1.10

回答

0

你不能在课堂上做任何类似的逻辑。显然你不能查询模型,直到他们都完全加载。

我不明白你要在那里做什么,但你应该把它移到一个方法。

+0

我想做出选择。我添加了相应的代码片段,以显示我想如何做到这一点。在你回答之后,我也发现了如何通过form来解决它。是否有可能在模型中以某种方式实现它,或者'form'是我的方式? – TitanFighter

+0

您应该在希望使用这些选择的表单中,在实例化它的位置执行此操作。 –

相关问题