> > On May 21, 3:30 am, James Tauber <[EMAIL PROTECTED]> wrote:
> >> I find myself being inconsistent in whether I put model helper
> >> methods
> >> on a custom manager or just as top-level functions in models.py
> >> [...]
> >> Any thoughts on best practices? Heuristics for deciding which to use?
>
> > James Bennett has a nice write up on this.
>
> >http://www.b-list.org/weblog/2008/feb/25/managers/
>
> Actually the post doesn't deal with my question at all. I'm not
> talking about class methods vs manager methods, but rather top-level
> (i.e. models module) functions vs manager methods.

Sorry, I misread your original post.

>
> django.contrib.auth.models.User.objects.create_user(...)
>         could have been
> django.contrib.auth.models.create_user(...)

I would favor the first of the two. Similarly, I would put
friends_for_user() as a FriendshipManager method in your original
example. Basically anything that acts on multiple Friendship model
instances (i.e. a table-level function) would qualify to be a
FriendshipManager method.

Utility functions that don't really need to go against the DB but are
used in conjunction with a model instance or model field would go as
module level functions. So, taking the same django.contrib.auth
example, the module level functions would be:

django.contrib.auth.models.get_hexdigest
django.contrib.auth.models.check_password

There's another subtle advantage to manager methods if you write 3rd
party apps. You can provide a new 'beta' manager implementation while
still providing the older implementation during a transition phase.
For example,

django.contrib.auth.models.User.objects
django.contrib.auth.models.User.beta_objects

And in a future release, beta_objects would be renamed to objects,
thus phasing out the old implementation.

In general, I would say that if a function seems natural at the module
level as well as at the manager level, it should probably be a manager
method.

-Rajesh
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to