Reputation: 10219
I'm new to Django and have a question about app structure and imports. My project structure looks like this (from deploydjango.com):
root/
manage.py
mysite/
__init__.py
urls.py
wsgi.py
settings/
apps/
__init__.py
profile
__init__.py
models.py
views.py
video
__init__.py
models.py
views.py
photos
__init__.py
models.py
views.py
Basically I will have some profiles on my site, where each profile then have a number of uploaded photos and videos. So in my video model I have the following code:
from django.db import models
from XXXXX import Profile
class Video(models.Model):
# more fields
company = models.ForeignKey(Profile, related_name='videos')
How do I import the Profile model for use in the Video model? Or is my structure not suitable..? What would be best practice for this?
Upvotes: 0
Views: 121
Reputation: 29794
I think your structure is fine. Basically, python's modules are just a python file which you may import like this: import <filename>
and you will have its functionality imported. But when you group some python files in a folder and include in that folder a file called init.py there you have a python package. With is a better way to manage a group of modules and it allows the dot notation in your imports. Django apps behave like packages as you may see.
If you want to import Profile
within your video
module then you have to do this:
from profile.models import Profile
That is, assuming that the Profile
model is defined in your profile
app in models.py
. That should do.
If you want more information about python project structure this is a great place.
Upvotes: 1