我更改了Request模型中的一些内容,并尝试运行makemigrations,它给了我这个错误
$python manage.py makemigrations
Traceback (most recent call last):
File "/Users/raymond/Documents/GitHub/Management/manage.py", line 22, in <module>
main()
File "/Users/raymond/Documents/GitHub/Management/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute
django.setup()
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/apps/config.py", line 300, in import_models
self.models_module = import_module(models_module_name)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/raymond/Documents/GitHub/Management/request/models.py", line 7, in <module>
class Request(models.Model):
File "/Users/raymond/Documents/GitHub/Management/request/models.py", line 9, in Request
teacher = models.ForeignKey(User, on_delete=models.CASCADE, choices=[(u.username, u) for u in User.objects.filter(profile__teacher_status=True)], related_name='teacher')
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/query.py", line 974, in filter
return self._filter_or_exclude(False, args, kwargs)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/query.py", line 992, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/query.py", line 999, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1375, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1396, in _add_q
child_clause, needed_inner = self.build_filter(
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1271, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1099, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1518, in names_to_path
*get_field_names_from_opts(opts),
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/sql/query.py", line 47, in get_field_names_from_opts
for f in opts.get_fields()
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/options.py", line 778, in get_fields
return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/options.py", line 838, in _get_fields
all_fields = self._relation_tree
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/options.py", line 751, in _relation_tree
return self._populate_directed_relation_graph()
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/db/models/options.py", line 721, in _populate_directed_relation_graph
all_models = self.apps.get_models(include_auto_created=True)
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/apps/registry.py", line 179, in get_models
self.check_models_ready()
File "/Users/raymond/opt/anaconda3/envs/python388/lib/python3.9/site-packages/django/apps/registry.py", line 141, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.发布于 2022-08-03 19:44:56
问题是您正在访问请求声明中的用户模型。
teacher = models.ForeignKey(User, on_delete=models.CASCADE, choices=[(u.username, u) for u in User.objects.filter(profile__teacher_status=True)], related_name='teacher')因为教师字段是用户的ForeignKey,所以不需要在模型中添加可用的选项。如果您确实需要这样做,您可以将其添加到正在使用的表单中,并将约束添加到请求模型中。
https://stackoverflow.com/questions/73226884
复制相似问题