On 5/1/2009 5:47 PM, mamco wrote: > In attempting to get a better grasp of Django, I'm attempting a fairly > simple timecard app. Multiple users of a small chain of retail > stores, where occasionally the employees jump between stores(company), > and between projects and activities. > > The interesting model is something like this: > > class Timecard(models.Model): > employee = models.ForeignKey(Employee) > company = models.ForeignKey(Company) > project = models.ForeignKey(Project) > activity = models.ForeignKey(Activity) > start = models.DateTimeField('The start time of the shift') > duration = models.DecimalField('The duration of the shift in > hours', max_digits=4, decimal_places=2) > notes = models.TextField('Special Notes on the shift') > > I've populated it with some data from a previous system (trying to > reproduce with Django), and I'm now trying to display a form which > shows a week at a time given a particular start date (which for now is > always a Sunday). > > I'm trying to show > employee, company, project on a single line followed by the totals > (summing the activity as its always 'default' for now) for Sun to Sat > and a total. > > So something like the following (pipes show columns for this post > purpose only): > J Smith | BranchA | Admin | 0.0 | 4.5 | 8.0 | 8.0 | 8.0 | 8.0 | 0.0 | > 36.5 > J Smith | BranchA | Training | 0.0 | 3.5 | 8.0 | 8.0 | 8.0 | 8.0 | 0.0 > | 36.5
I always like to represent code as closely to how I think first and then optimize. There are probably more clever ways of achieving this, but this is generally how I would approach it (in pseudo code): {{{ sd = datetime.date(start_date) ed = datetime.date(end_date) # Iterate to populat this, obviously date_header = { datetime.date(first_day): 0, datetime.date(second_day): 1, datetime.date(third_day): 2, ... } entries = TC.objects.range_filtered.values_list( 'employee', 'company', 'project').order_by(...) schedule = {} timecards = Timecards.objects.range_filtered for t in timecards: info = schedule.setdefault((t.employee, t.company, t.project), {}) by_day = info.setdefault('by_day', [0]*len(date_header)) i = date_header[datetime.date(t.start)] by_day[i] += t.duration info['total'] = info.get('total', 0) + t.duration }}} Pass `entries` and `schedule` as context vars to your template, and you can iterate through your schedule using ordered list of entries. You get the idea. -- George --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---