On Nov 7, 7:13 pm, John M <[EMAIL PROTECTED]> wrote:
> I wanted to get some feedback on how I'm using custom model managers.
>
> I've put all my queries into one manager, each in a different method.
> Is this the right way to go?
>
> So for example:
>
> CHOICES_TASK = (
>                         ("NO", "None"),
>                         ("GR", "Green"),
>                         ("YL", "Yellow"),
>                         ("RD", "Red"),
>                 )
>
> class TaskManager(models.Manager):
>         use_for_related_fields = True
>
>         # Task.objects.all()
>         def get_query_set(self):
>                 return super(TaskManager, self).get_query_set()
>
>         # Task.milestones()
>         def Milestones(self):
>                 return super(TaskManager,
> self).get_query_set().filter(milestone=True)
>
>         def Accomplishments(self):
>                 return super(TaskManager,
> self).get_query_set().filter(milestone=False).filter(completed=True)
>
>         def Nextsteps(self):
>                 return super(TaskManager,
> self).get_query_set().filter(milestone=False).filter(completed=False)
>
> class Task(models.Model):
>         report = models.ForeignKey(Report)
>         name = models.CharField(max_length=50)
>         started = models.BooleanField(default=False)
>         status = models.CharField(max_length=20, choices=CHOICES_TASK,
> default="NO")
>         completed = models.BooleanField(default=False)
>         duedate = models.DateField(blank=True, null=True)
>         milestone = models.BooleanField(default=False)
>
>         # Managers
>         objects = TaskManager()
>         milestones = TaskManager().Milestones
>         accomplishments = TaskManager().Accomplishments
>         nextsteps = TaskManager().Nextsteps
>
>         def __unicode__(self):
>                 return self.name


There's nothing wrong with the general idea - that's how I do it
myself, although the official docs say to use multiple managers and
override get_query_set on each one. A couple of comments on your
implementation though:

1. There's no point in defining a get_query_set model simply to pass
to super. If you don't define it at all, it will just inherit from the
parent class anyway, which is exactly the same as what happens in your
version, so you may as well save two lines of code.

2. The convention in Python/Django is to have all lower case names for
functions and methods - so it should be milestones, accomplishments,
etc.

3. I don't think there's any point in defining all the extra manager
attributes that you have. Leave objects as TaskManager, then you can
do things like:
Task.objects.milestones()
Task.objects.accomplishments().filter(started=False)
or whatever.

--
DR.
--~--~---------~--~----~------------~-------~--~----~
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