Lesen Sie alle Themen bei StackOverflow und Internet - kein Glück.Wie können Sie Entscheidungen basierend auf dem Modell treffen? - Modelle sind noch nicht geladen. FK-Problem (scheint zu sein)
# 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:
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.
Dies geschieht, wenn ich irgendwelche manage.py
s Befehl ausgeführt werden soll.
Aus wenigen Tests oben kann ich erraten, dass das Problem auf ForeignKey zurückzuführen ist. Wie man es repariert?
Django 1.10
Ich möchte Entscheidungen treffen. Ich habe entsprechende Code-Stücke hinzugefügt, um zu zeigen, wie ich das machen wollte. Nach deiner Antwort habe ich auch herausgefunden, wie man es über 'form' lösen kann. Ist es möglich, es irgendwie in Modellen zu implementieren oder "Form" ist mein Weg? – TitanFighter
Sie sollten das in dem Formular tun, wo Sie diese Auswahlmöglichkeiten an dem Punkt verwenden möchten, an dem Sie es instanziieren. –