Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide how to split up your Django project into apps

Tags:

django

I have a project that I wrote in PHP/symfony that uses 45 tables. I'm in the process of porting it to Python/Django.

There's a belief I've held for years that you should split up your projects into a bunch of small files rather than a few huge files. From what I understand, that's not an odd thing to believe. In Rails and symfony, there's a one-model-per-file convention. In Django, however, it seems that most developers put all of each app's models in one file.

This makes sense to me if your apps are each small enough. It doesn't make sense to me for large apps, though, and what I have is at least one large app.

Out of the 45 tables my project uses, 35 are closely related. I have a script that imports data from CSV files. For each line in each CSV file, I save 50-80 pieces of data into 30-35 different tables in one fell swoop.

Maybe I'm just thinking about this the wrong way but it would seem incredibly odd to me to divide my project into 6 or 7 different apps when almost all my tables are inextricably linked. When I touch one table, I touch all 35 tables. The delineations would have to be arbitrary. What would be the point of that?

Please forgive me if I come off as biased because I certainly am biased. I'm not having this problem in symfony and I wouldn't be having it in Rails. (I chose Django because of GeoDjango and Python's GIS capabilities.)

  • In a perfect world, I would have one model per file.
  • If I try to have one model per file, I get circular reference problems.
  • I could avoid the circular reference problems by putting all my models in one file but that feels wrong to me.
  • I could avoid putting all my models in the same file by splitting them into separate apps, but in order to end up with sufficiently small apps, I'd have to break up my project in arbitrary (and therefore pointless) ways.

What should I do?

like image 452
Jason Swett Avatar asked Feb 04 '11 18:02

Jason Swett


People also ask

Can a Django project have multiple apps?

Any app can use any other app's code, including models. And you can have as many models as you like in a single app.

How many apps can a Django project have?

Django comes with six built-in apps that we can examine.

What is the difference between a project and an app in Django?

A project refers to the entire application and all its parts. An app refers to a submodule of the project. It's self-sufficient and not intertwined with the other apps in the project such that, in theory, you could pick it up and plop it down into another project without any modification.


1 Answers

If having one model per file would be the perfect answer for you, there's an app for that.

I've never done it on a scale of 80 model files but I can certainly point you towards another stack question:
About 20 models in 1 django app

http://djangosnippets.org/snippets/1838/

What kind of circular reference problems are you having by the way? If it's with ForeignKey definitions, here's a way around that... http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey

You can also look into django.db.loading.get_model, but some may frown on this.

like image 90
Yuji 'Tomita' Tomita Avatar answered Oct 10 '22 15:10

Yuji 'Tomita' Tomita