To alleviate models.py file growing large you can split it up like so

/models/
/models/__init__.py
/models/invoices.py
/models/products.py
/models/taxes.py
...

The are two tricks to the above to make it work however; first you must import your files in the __init__.py and manually specify the app_label for all your model files:

# /models__init__.py
from .taxes import *
from .products import *
from .sandh_costs import *
from .discounts import

# /models/taxes.py
class DiscountBase(models.Model):
   ...
   ### model options - "anything that's not a field"
   class Meta:
app_label = 'pnpstore' # must specify same app label so that in admin models are grouped correctly

I also split views in a similar manner.


-----Original Message----- From: Javier Guerra Giraldez
Sent: Monday, April 23, 2012 9:45 AM
To: django-users@googlegroups.com
Subject: Re: keep models simple ? where to put the business logic ?

On Mon, Apr 23, 2012 at 6:47 AM, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
Models - like any other module - should indeed be "as simple as
possible", _but not simpler_. If you concern is about your models.py
file size, it might be time to refactor it into a package.

or maybe the app has to be more focused, and split in two or more apps.


Now if there are parts of your methods that are low-level enough and
don't really need to know about your models, yeps, they may belong to
some "model-agnostic" utility module.

also when the usercase concepts are not exactly the same as the
database records. then another model-like layer can be useful.

for example, lets say you're working with a genealogy application, and
you have a Person model, and several kinds of relationships between
person instances.   But let's also say that you have a 'Family'
concept that is easy to derive from the database (so it doesn't need
another model class), but you want to add some extra behaviour at the
Family level (above Person and Relationship).   then it might be
useful to add a new Family.py module that works on your models and is
managed by the views in similar ways to them.


but in the end, yes: the vast majority of business logic belongs in
models.py files, definitely not in the views.

--
Javier

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@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.

Daniel Sokolowski
Web Engineer
KL Insight
http://klinsight.com/
Tel: 613-344-2116 | Fax: 613.634.7029
993 Princess Street, Suite 212
Kingston, ON K7L 1H3, Canada
--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@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