2016-09-22 45 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

您應該在希望使用這些選擇的表單中,在實例化它的位置執行此操作。 –

相關問題