3 ms·
I'm not originally a Python guy, but when I’m forced to work with Django, what I do for models and settings and such is just creating a barrel module, that is—i
by 9dev 2y ago
I'm not originally a Python guy, but when I’m forced to work with Django, what I do for models and settings and such is just creating a barrel module, that is—instead of models.py, have models/__init__.py that imports (and thus re-exports) everything from every file in the models folder. Then I can have a single model per file, as it should be.
I’ll never understand the conventions of Python land, but at least the language is flexible enough to do it properly.
- bena 2y agoYes, that's the way to do it. Python is fairly accepting with modules being either directories or files. Going the dir/__init__.py route also allows you to do a bit of encapsulation as you can not export things. Of course, someone could import from your file directly still. But, yeah, there is no reason to keep all of your models in a single file.
- adithyareddy 2y agoZulip does this: https://github.com/zulip/zulip/blob/main/zerver/models/__init__.py https://github.com/zulip/zulip/blob/main/zerver/models/__ini... Zulip in general is a great example of a large open source Django app that's been maintained and actively developed for a long time. I use it as a reference quite a lot.
- curiousgal 2y agoWhy are they using the as?