Hacker News new | past | comments | ask | show | jobs | submit login

As a longtime Rails developer who has experimented with Python but knew little about Django, I read this article with interest. Something that jumped out at me was the author's description of the "models.py file, which contains all the application models (multiple models in a single file)". I did some quick research and I gather that this approach isn't maintained as you move beyond the scale of a toy application. I kind of think he's doing Django a disservice with his current wording there, as my immediate reaction was that it sounded nightmarish!



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.


We have a models file that imports models from other files, to keep them in the same directory tree. Anyway one could import models from any location of the project. Every app the Django project is made from can have its own urls, models, migrations.


You have dozens of apps (modules) each having its own models.py with a handful of models.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: