11 ms·
The projects/app relationship never 'clicked' with me. I also have a dislike for the awkward 'polls' example in the django tutorial. That said, I like Django.
by bluedino 8y ago
The projects/app relationship never 'clicked' with me. I also have a dislike for the awkward 'polls' example in the django tutorial.
That said, I like Django. Of course, I liked Rails better, but maybe that's because it was my 'first' and I never used any of the big php frameworks for a long period of time.
I quit wasting my time with Flask for projects a while ago. It's a really great project and suited for smaller things, but you just end up re-implenting django. I'd rather someone made those decision for me.
- deleted 8y ago[deleted]
- digitaltrees 8y agoI feel the same way. Does any one have a good overview on how the community actually separates functionality into “apps”? I have been looking at a rails like framework in python called Masonite. Does anyone in the python/Django community think this will gain traction?
- travisjungroth 8y agoThe separate apps thing is overblown (although that sentiment seems to be shared in these comments). If you're making a monolith web app, you can be safe just chucking it into one django app. If you're concerned about code organization, you can do that in one app with either sub apps or just turning files into packages later. I think this is a more productive strategy for django beginners. If you've identified some clean, generic problem where you want to reuse the code or open source it, make it a separate app. If you have a web site with areas that barely interact with each other, make it into separate apps (a chef's website where the recipes are separate from the cooking classes calendar). If you know what you're doing and feel like separate apps will help with organization, then split them up as well. Otherwise, one is fine.
- ergo14 8y agoI think pyramid or flask has more traction nowdays.
- Alex3917 8y ago> Does any one have a good overview on how the community actually separates functionality into “apps”? For the most part there isn't any need to, with the exception of things explicitly meant to be shared. So if you are a large company and want a bunch of custom auth logic or something to be shared among many different teams running their own Django service, then you might want to build that as an app. Or else if you were building something as an open source project you might want to do the same. In terms of separating apps by functionality, I think the main reason many people do it is because they are under the mistaken impression that putting things into different apps will make them magically re-usable. That's not to say you shouldn't do it, only that I think most people do it for the wrong reasons. In general I would say don't bother splitting up your project into different apps by functionality until you have too many models to comfortably fit into one file. That might not be until 20KLOC. For apps that are less than 10KLOC, I think in most cases the overhead probably outweighs the benefits. There isn't really any hard rule one how to do this, but you might do something like: - Account management app for creating and deleting users, getting user profiles, password resets, etc. - Apps for integrations with third-party APIs, e.g. Duns or Mailgun. - Apps for different front doors, e.g. if you make a Facebook chat bot that's powered by the same tech that powers your normal website. And then after that you can possibly split up your business logic of your core website into different logical components, but I would do that last. By the time you get there you should start to have a feel for how to do this. The one place I use it for our own startup is that we recently built a couple different front doors for our site, and each one accepts data in a slightly different format and has different authentication requirements. I put each of these in their own apps, but everything else is just in one main app.
- hrayr 8y ago> until you have too many models to comfortably fit into one file. That might not be until 20KLOC. Even for larger apps in my django project, I convert my models into a package and split all my models into individual files (same with views and serializers for DRF). This keeps my models easier to manage.
- mattrp 8y agoApps can be a good way to generalize a set of classes for reuse in other projects. For instance we put auth into an app so if we fire up other projects we don’t have to rewrite auth.
- systemspeed 8y agoAs a Django enthusiast and python programmer, I often want less magic from Django, not more. This makes pyramid or flask seem tempting to me. If I wanted Rails that badly, I would simply port my code to Ruby.
- Nextgrid 8y agoDjango is supposed to be the “Rails” of Python, allowing programmers who want a Rails-like experience to continue using Python instead of switching languages completely. If you don’t want “magic” then your solution is right - just use Flask.
- crimsonalucard 8y agoThe thing with the "magic" of django is that you don't need to use it! It's up to you whether you want to use the magic or not! The magic is powerful. The thing that turns users away is learning the magic.
- thijsvandien 8y agoDjango's supposedly independent components were built with the context of the rest of Django in mind and have all kinds of default behaviors based on that. Surely they may work without the rest, but it isn't nearly as smooth as working with independent libraries that were designed with a much more limited scope.
- bildung 8y ago> Django is supposed to be the “Rails” of Python, allowing programmers who want a Rails-like experience to continue using Python instead of switching languages completely. A small nitpick: While Django may indeed fill that role, it's actually older than Rails, so wasn't "supposed" to do that. Both projects have been created independently of each other.
- Scarblac 8y agoThey're just modules by another name. The way I think about design is usually centered around the data model, so if some subject has a number of ORM models and is pretty self contained it makes sense to put them in their own app, together with related urls and views and stuff. They still live in the same repository and aren't meant to be re-used, just split into logical parts to make the code base easier to think about. Modules, in other words.
- jarcoal 8y agoI don't use multiple apps anymore and instead treat the project as the app, so my directory structure looks something like this ('project' can change to something specific to what you're working on): * manage.py * project/settings.py * project/urls.py * project/models/ (each model gets it's own file here) * project/views/ (same for views) ...and so on. And in `INSTALLED_APPS` I just add 'project'. Makes it so I don't waste a bunch of time trying to figure out which app to put a model in, etc.
- travisjungroth 8y agoI've never gone so far as each model getting its own file. Interesting idea.
- digitaltrees 8y agoThat looks like rails. What made you settle on this given that Django promotes a different structure? I am curious as I like this approach as well, but would be nervous about making life harder by not following the framework and community path.
- jarcoal 8y agoBeen building Django apps for almost 10 years and after seeing the amount of wasted time going into organizing things into apps, I settled on this technique. It doesn’t really swim against the current as long as you import models into the __init__.py file in their package.
- Scarblac 8y agoWe do this too on some projects, it's painless. As far as I know Django doesn't really have an opinion on this, you have a lot of freedom in how you organize your project.
- spapas82 8y agoThere's no hard rules about app separation. Here are some from my experience: - You may have stuff that may be re-used for multiple projects. For example the departments of your organization and their attributes; these should be extracted to apps so they can be easily used for other projects. Of course this would make sense if you are a software house or an organization that need multiple apps (i.e a bank) not if you are working for a single application. - Django recommends using a custom user model. I put that in a separate app along with any related classes like user preferences etc. See also my previous bullet. - Non related models. If you add a model that doesn't have any relation to the rest of your models then maybe you should move it to a different app? For example in one of my applications I have a NewsItem model that is just a bunch of notes that the admins can add and will be visible to the homepage; there's no need to put it in with the rest of the app (of course this also obeys the 1st bullet) - Beyond that, usually you'll understand when you have to separate your apps. You don't need to force it, I know of huge apps with a models or views py with thousands of lines (of course in such cases even if you think that these should belong to the same app nothing's stopping you from making them a package instead of a module). However you'll usually come to a point where you think that for your reasons you need to separate
- danpalmer 8y agoWe split into multiple apps within one project/site for separation of concerns instead of reusability. We have ~370 apps and nest them quite heavily, probably ~30 at the top level. This works well for us and we've managed to scale nicely to 15 people working on the codebase quite nicely.
- kumarvvr 8y agoWhat do you mean by nesting apps? Like group them into domain folders and have multiple apps within a domain folder?
- bildung 8y agoI assume Dan really means a nested directory structure, like so: django_project/top_level_app/second_level_app/ in top_level_app you then could do from second_level_app.models import model1 or from a different top level app: from other_top_level_app.second_level_app.mixins import a_mixin
- danpalmer 8y agoYep, exactly this. For example: project/orders/orders_shipping/orders_ups Unfortunately app names have a global namespace within Django's model registry, so we often have to prefix nested apps. The place where this really works nicely is user facing features and internal editing interfaces, or in features where we have N of a feature that are all similar. For the former, we might have an app such as "blog", and then a nested app within that called "blog_editor". This way we can separate blog models, business logic and user-facing behaviour, from CRUD interfaces designed for internal users. In the latter, if we have 3 shipping providers, we might have an app "shipping" with the core shipping models, with nested apps for "ups", "royal_mail", "dpd", etc, each of which might have the necessary models needed to manage uploading to their APIs, business logic, etc. There may be a common-ish interface exposed by these apps. This structure is probably the thing I like most about Django over, say, Rails. It's not necessary for all Django projects, but I think it's pretty nice.
- kumarvvr 8y agoTwo Scoops books about Django have awesome tips and tricks about many topucs and architecture is one of them. Even if the latest version is for 1.11, the concepts are relevant.
- kumarvvr 8y agoFor small projects, I usually have two apps, the frontend and backend apps.
- theptip 8y ago> The projects/app relationship never 'clicked' with me. This is an area that I think is poorly documented (both in the otherwise-great official docs and in the broader community). My advice for beginners is to just create an app called `core` and put everything there, until you have a reason to do something else. (I'd also propose putting your DJANGO_SETTINGS_MODULE aka "project name" as "django_settings" and only put your Django settings in there, but that's a little more esoteric and I'd not recommend that to a newbie in order to minimize confusion.) In general, I think you should take the "monolith first" strategy (e.g. https://martinfowler.com/bliki/MonolithFirst.html https://martinfowler.com/bliki/MonolithFirst.html) and just use a single app, unless you're writing something that is obviously going to be reused elsewhere. The alternative is to attempt to define service boundaries before you know what your app's structure looks like; you'll inevitably get it wrong. And migrating models between apps is incredibly painful if you have foreign keys spanning apps. I've also found a couple bugs in Django's migration machinery that only affect multi-app foreign key relationships; I've never seen a single-app migration bug. So there's a real cost to going down the multi-app path, which you need to balance against any benefits (I've seen none). (Credentials/disclaimer; startup using DRF in prod for 3.5 years, mature domain model with ~50 model classes, feeling the pain of incorrectly choosing 5 apps in the initial setup instead of just using one. I'm sure there's a good argument for apps when you get to a larger scale and are sharing components/functionality between teams.)
- andrewstuart 8y ago>> I quit wasting my time with Flask for projects a while ago. It's a really great project and suited for smaller things, but you just end up re-implenting django This is spot on .... Flask is great, but it seems that to get anything substantial done with Flask you must first reimplement big chunks of Djangoesque functionality in it - why not just jump straight to Django?
- AdamM12 8y agoYeah that or just plug in 5-6 extensions and you end up with the same. Found myself doing flask-sqlalchemy, flask-migrate, etc. after a while.
- jamestimmins 8y agoMozilla has some large, public Django projects on Github that are worth taking a look at to see how they're splitting up apps. https://github.com/mozilla/addons-server https://github.com/mozilla/addons-server