Я хочу создать модель под названием задача, у нее есть несколько полей и связь с самим собой, это означает, что сама задача может быть создана из некоторых других задач, код:
from django.db import models
class Task(models.Model):
name = models.CharField(max_length=100)
...
...
tasks = models.ForeignKey(Task, on_delete=models.CASCADE, blank=True)
def __str__(self):
return self.name
, а затем django показываетошибки, что Задача не определена:
/home/hamex/currect/myBlog/main/task/models.py changed, reloading.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
raise _exception[1]
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/hamex/virtualPyEviors/django2/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.6/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 "/home/hamex/currect/myBlog/main/task/models.py", line 14, in <module>
class Task(models.Model):
File "/home/hamex/currect/myBlog/main/task/models.py", line 23, in Task
testwithselffor = models.ForeignKey(Task, blank=True, on_delete=models.CASCADE)
NameError: name 'Task' is not defined
я делаю что-то не так ??а может это не правильный способ решить эту проблему ??