Я пытаюсь ограничить поле в одной из моих моделей выбором всех пользователей, которые являются членами определенной группы.В другом поле у всех зарегистрированных пользователей есть выбор.Это прекрасно работает в оболочке:
>>> from django.contrib.auth.models import User, Group
>>> all_users = User.objects.all()
>>> all_users
<QuerySet [<User: somedude>, <User: test>, <User: testma>]>
>>> tm_users = User.objects.filter(groups__name='Testmanager')
>>> tm_users
<QuerySet [<User: somedude>, <User: testma>]>
Но если я добавлю тот же код в модель:
class TestPlan(models.Model):
all_users = User.objects.all()
all_user_choices = ((x.username, x.get_full_name()) for x in all_users)
tm_users = User.objects.filter(groups__name='Testmanager')
tm_user_choices = ((x.username, x.get_full_name()) for x in tm_users)
tp_title = models.CharField(max_length=200, verbose_name='Title')
tp_manager = models.CharField(max_length=100, blank=True,
verbose_name='Test Manager',
choices=tm_user_choices)
tp_tester = models.CharField(max_length=100, blank=True,
verbose_name='Tester',
choices=all_user_choices)
def __str__(self):
return f'{self.tp_title}'
, я получу следующее менее полезное (для меня) трассирование стека:
O:\somedude\Documents\Python\Django\testcases>manage.py makemigrations
Traceback (most recent call last):
File "O:\somedude\Documents\Python\Django\testcases\manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 381, i
n execute_from_command_line
utility.execute()
File "C:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 357, i
n execute
django.setup()
File "C:\Program Files\Python36\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Program Files\Python36\lib\site-packages\django\apps\config.py", line 198, in import_mode
ls
self.models_module = import_module(models_module_name)
File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "O:\somedude\Documents\Python\Django\testcases\testman\models.py", line 68, in <module>
class TestPlan(models.Model):
File "O:\somedude\Documents\Python\Django\testcases\testman\models.py", line 70, in TestPlan
tm_users = User.objects.filter(groups__name='Testmanager')
File "C:\Program Files\Python36\lib\site-packages\django\db\models\manager.py", line 82, in manage
r_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py", line 844, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\query.py", line 862, in _filter
_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1263, in ad
d_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1287, in _a
dd_q
split_subq=split_subq,
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1164, in bu
ild_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1028, in so
lve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "C:\Program Files\Python36\lib\site-packages\django\db\models\sql\query.py", line 1365, in na
mes_to_path
if field.is_relation and not field.related_model:
File "C:\Program Files\Python36\lib\site-packages\django\utils\functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\related.py", line 94, in
related_model
apps.check_models_ready()
File "C:\Program Files\Python36\lib\site-packages\django\apps\registry.py", line 137, in check_mod
els_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
O:\somedude\Documents\Python\Django\testcases>
Это работает, когда я выполняю User.objects.all (), но падает, когда я пытаюсь фильтровать пользователей по группам.Я действительно понятия не имею, что я делаю здесь не так.Это ошибка или особенность?