[Django]-Import strategy within django applications

3👍

✅

The second approach assumes that . is in sys.path before any other directories that may contain a models module. There is no requirement that . be in it at all, so importing either via relative imports or via the app is best.

3👍

I personally, try to keep the convention of always importing from the app.

Don’t import from the project, because project name can change; your app can be used in some other project (at least you are supposed to make apps like that).

Don’t import from models directly because, as Ignacio rightly mentions, it is not necessary that . is in the python path.

But, App names are always on the python path. Django adds them to the python path (via set_environ(settings)), on the top of the list, so you can be rest assured that the right files are always picked up.

Leave a comment