Op maandag 27 september 2010 09:08:53 schreef MrMuffin:
> Where do you put your business logic in django? In my project I`ve put
> it into the models.py, but that file soon become huge and hard to
> maintain. Of course I can just stuff it into whatever file I like, but
> I`d like to have some standard way of doing this. There seems to be
> something missing in django when it comes to business logic. It`s all
> model, views and templates and that`s all great for small projects,
> but both the models.py and views.py very soon gets huge and how do you
> re-organize your project when that happens? Splitting views and models
> into seperate files is only a partial solution and that requires some
> hackish code in __init__.py to make syncdb etc work. And then there`s
> urls.py.

I'm surprised no one mentioned manager classes yet. This is an example for 
models.py:

  from django.db import models
  from yourapp.managers import SomeObjectManager

  class SomeObject(models.Model):
    objects = SomeObjectManager()


Now you can do things like: SomeObject.objects.foo() which is defined in your 
manager class. This allows a much better separation of logic and models.

 
> Should there be a better and standardized way to organize huge
> projects in django?

Yes. Making multiple applications. I highly recommend watching this DjangoCon 
video by James Bennett: http://www.youtube.com/watch?v=A-S0tqpPga4

It explains how apps can be made reusable. For example a "blog" application is 
actually not 1 monolythic application, but 8+ tiny apps. User registration and 
account management can also be written as 4+ little apps.

SInce watching that video, my projects are composed by default of 2 apps. A 
"projectname" for the core backend stuff, and a "projectname-site" with the 
templates, settings, and frontend media. This is imho a nicer base to start 
"hooking in" more applications.

More functionality (tagging, rating, accounts, etc.. will all become separate 
apps) which will be hooked in the "projectname-site" project. In case you 
wonder, http://www.djangopackages.com/ gives you quite a wealth of small 
reusable parts that can be hooked in directly.

Greetings,

Diederik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to