开发者

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

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开发者_JS百科 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜