I'm trying to split the models.py into multiple files inside a folder.
what is the proper way to do that ?
all the methods in the internet from 8 years ago and it's not working now.
test1
__init__.py
admin.py
apps.py
tests.py
views.py
migrations
models
__init__.py
comment.py
like.py
post.py
profile.py
inside init.py
from django.db import models
from django.conf import settings
from .like import Like
from .post import Post
from .profile import Profile
from .comment import Comment
inside comment.py
class Comment(models.Model):
commented_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
for_post = models.ForeignKey(Post, on_delete=models.CASCADE)
inside like.py
class Like(models.Model):
liked_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
inside post.py
class Post(models.Model):
posted_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
inside profile.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
when I try to makemigrations:
python manage.py makemigrations test1
I got this error:
(sandbox-qFsmxchL) λ python manage.py makemigrations test1
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\USER\.virtualenvs\sandbox-qFsmxchL\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\USER\.virtualenvs\sandbox-qFsmxchL\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\USER\.virtualenvs\sandbox-qFsmxchL\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\USER\.virtualenvs\sandbox-qFsmxchL\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\USER\.virtualenvs\sandbox-qFsmxchL\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "c:\users\USER\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\USER\Desktop\sandbox\test1\models\__init__.py", line 4, in <module>
from .like import Like
File "C:\Users\USER\Desktop\sandbox\test1\models\like.py", line 2, in <module>
class Like(models.Model):
NameError: name 'models' is not defined
You can do that putting them into a models folder, like:
models/
- __init__.py
- model_1.py
- model_2.py
and __init__.py should import all models contained in the other files
from .model_1 import Model1
from .model_2 import Model2
It is up to you to split them, depending on if you have a lot of models and if those are stricly related to each others.
Edit:
inside init.py
from .post import Post
from .like import Like
from .profile import Profile
from .comment import Comment
inside comment.py
from django.db import models
from django.conf import settings
from test1.models import Post
class Comment(models.Model):
commented_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
for_post = models.ForeignKey(Post, on_delete=models.CASCADE)
inside like.py
from django.db import models
from django.conf import settings
from test1.models import Post
class Like(models.Model):
liked_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
inside post.py
from django.db import models
from django.conf import settings
class Post(models.Model):
posted_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
inside profile.py
from django.db import models
from django.conf import settings
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With