On Jun 3, 12:28 am, Streamweaver <[email protected]> wrote:
> I'm pretty new to Django still and I know much is still escaping me.
> In particular I'm having trouble still with how to query subsets of
> related objects.
>
> In this case I have two Models.
>
> class Project(models.Model):
> title = models.CharField(max_length=141)
> ...
>
> class Release(models.Model):
> project_fk = models.ForeignKey(Project)
> point_estimate = models.IntegerField(choices=ESTIMATE_CHOICES,
> null=True, blank=True)
> velocity_points = models.IntegerField(choices=VELOCITY_CHOICES,
> null=True, blank=True)
> ...
>
> What I was looking to do is create a way sum the total number of
> velocity points in releases related to a Project. I also wanted to
> sum the total number of point_estimate points related to a Project.
>
> From what I see a Custom Manager would seem necessary but I'm getting
> confused on related objects and aggregating items. This is for Django
> v 1.0 and would not include v1.1 aggregation features.
>
> Thanks in advance for any insight.
If you don't want to write custom SQL - which might actually be the
best way of doing it if you're not using 1.1 - you could get the
values for all the related velocity points and sum them in Python. But
I wouldn't put this in a manger - it probably goes in a method on the
Project model:
class Project:
...
def get_velocity_total(self):
velocities = self.release_set.values_list('velocity_points',
flat=True)
return sum(velocities)
And you'd call it with:
myproject.get_velocity_total()
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---