Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



Zulip does this: 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.


Why are they using the as?


Yes, 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: