G’day all,
My project has grown considerably in the last six months and I’m currently working through a major uplift to tidy up the codebase before first semester start after summer (Northern Hemisphere).
Almost everything in my app relates to a case. That is, a case has physicals, it has diagnostics, it has treatments and it has many other things.
Additionally, a user performs actions and these actions are recorded. For example, a user chooses a treatment and a treatment has a relationship to a case, therefore the users actions are related to cases. Everywhere I look, relationships always end up back with the case. And to me, this is logical, we’re modelling a case, e.g. The case of the bunny rabbit with a sore foot who was treated by Jenny.
As my code base has grown, I’ve tried to keep things tidy by splitting things out into different apps. For example, the diagnostics part of a case is sizeable in terms of models, views and utilities. I like this, as it keeps thing quite clean and my files lengths are kept to a manageable level.
But as the project grows, I see some downsides to this approach. My URLs are getting a bit too fiddly for my liking and would be much simpler if everything that was related to a case was in the cases app.
So with that in mind, I’ve been thinking about different project structures for my app.
A Single App with single files I don’t like the thought of this one. My models file would be huge as would my views file etc etc etc. The only positives are simple URL patterns and that I can now reuse this app in another Django project. I have no intention of going down this route.
A Single App with multiple views, model, etc files In some of my apps I have a module for serializers and tests instead of single app level files. I like this level of abstractions as my file names can be very concise - nested_serializers.py
, censored_serializers.py
and admin_serializers.py
as some examples. This keeps line lengths to a maneable level, especially with test files.
The drawback here would be that my modules will contain a lot of files. A lot! The upside is that I again have a reusable app, simpler URLs and everything that is related to a case is neatly packed up in the cases app. It’s the “a lot!” of files which deters me from this path.
A Single App with Sub Apps This is an idea which occurred to me recently but I have no experience with it and not even sure if it is the done thing or recommended.
My thinking is that I would have an app, cases
and in cases
I would have sub-apps for all the major components, i.e. diagnostics
, treatments
etc.
By doing this I would gain a reusable cases app, simpler URLs than what I have today and a less chaotic file structure than the two structures already mentioned. I’m not sure if there are any downsides to this which is ringing alarm bells, and hence why I’m writing this post when I instead should be enjoying lunch and a cup of tea.
Keep Things as they Are
Of course, I can keep things as they are, but I wouldn’t be writing this if there weren’t some nagging thoughts in my head. At this point in time, this is OK. Little refactoring but I have this gnawing feeling that something just isn’t quite right. I’ve read in Two Scoops of Django that the authors recommend keeping model files small and manageable. I’ve also read that one should keep all of their models which relate to each other in the same app. Both of these make sense to me, but both these things cannot be true in my project. I also appreciated that no two Django projects are necessarily the same, and with that in mind I’m trying to find what works best for my project and not necessarily trying to follow rules and recommendations to the letter.
API on the side My project is for better or worse an SPA. I use DRF and think it is mighty. Currently, all my apps have their API urls in their respective apps. I quite like this as opposed to what I have seen others do, which is to have a separate app for their API. This app typically has no models and contains just the API side of things, e.g. serializers, views, and URLs. Doing it this way seems to be much the same as building a largish Django project with just one app. Eventually the API app will fill up with many, many lines of code and start to be a bit cumbersome to work with - at least that’s what I’m thinking
So with the above said, I think I’m going to stick with my approach of including the API URLs inside each respective app. To me, this makes a lot of sense because the absolute vast majority of my URLs are DRF URLs and the apps are 99% DRF apps.
Recommendations So I’m wondering how others have tackled the issues that I am facing now. What have you done with your apps when you’ve had 50+ models all related back to a single model? Have you gone down any of the routes I have mentioned, or have you chosen another path? I’d be very interested to read what you have done.
As always, thank you for taking the time to lend a helping hand.
C