Re: Displaying rows as columns
thanks George - working on it - nearly there. On May 2, 12:17 pm, George Songwrote: > On 5/2/2009 7:40 AM, George Song wrote: > > > Pass `entries` and `schedule` as context vars to your template, and > > you can iterate through your schedule using ordered list of entries. > > There's also `django.utils.datastructures.SortedDict` so you can bypass > the `entries` business and save yourself another query. > > -- > 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 -~--~~~~--~~--~--~---
Re: Displaying rows as columns
On 5/2/2009 7:40 AM, George Song wrote: > Pass `entries` and `schedule` as context vars to your template, and > you can iterate through your schedule using ordered list of entries. There's also `django.utils.datastructures.SortedDict` so you can bypass the `entries` business and save yourself another query. -- 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 -~--~~~~--~~--~--~---
Re: Displaying rows as columns
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 -~--~~~~--~~--~--~---
Re: Displaying rows as columns
@George great question. In a very long ago php implementation of something similar with Access as a backend, I simply queried the date range in question, and built up an array, then walked through that while building the html table. It was ugly and even had some queries right in along the tr/td tags to go look up what was budgeted for that particular task and coloured the row accordingly for over/under budget. All accomplished in one big ole procedural ugly php file without any separation of logic and content. In another environment with Cold Fusion and Informix as the db, a not too complex single sql statement handled it all with case statements, and a bit of a custom informix function/stored procedure handling the totals versus budgeted. In the informix implementation calls to the database were 'expensive' so attempts were made to do everything in a single sql statement where possible. So I'm open to what would be the sort of best practice taking advantage of python's strengths along with a mysql db, keeping in mind that that the db may switch to postgresql at some point (trying to stay away from non-standard sql calls). @Lars I like the idea of the a function like get_week as that allows for future needs like get_month, get_day if I have something like start_date = datetime.date(2009, 5, 3) end_date = datetime.date(2009, 5, 9) Timecard.objects.filter(start__range=(start_date, end_date)) my question is more of the what is the best approach to take to then display the data outlined in the original post. I provided the model and the specific implementation as it seems that lots of times that information gets requested instead of the generic details. The more I read through the documentation, the more I find very specific functions which could make life very simple. Having no real experience with Python outside of following along with tutorials, I am asking here for advice. Is there an 'absolute beginner' section where this would be better posted? If I want to put the week at a time into python structure, which structure would be recommended, should I let the db sort the data or not bother and build the equivalent of an multi-dimensional array, then sort it? It wouldn't surprise me that someone has a function already written (I've looked on djangosnippets) which does just this which I could use as a starting point. I do not want to change the model for this presentation requirement. Everything else is much easier with the model in this way. The timecards will be subsequently be sent to a journal table, used for billing/cost analysis. Being able to bring up detail for a particular day in the timecard table without the rest of the week as well has served quite well. The supervisor approval process will also be able to reject a particular entry for a particular task while allowing everything else for that pay period - the current model allows for that (along with some other tables). Having a whole week of time delayed for billing would not work out for this implementation. I appreciate the suggestion though. I'm also planning on pulling in a few years worth of data which is currently stored in a very similar format - would love to keep that process as easy as possible. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Displaying rows as columns
mamco wrote: > Hi, > 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 > > Being new to Python and Django, I can't seem to get a function written > to kick this output out. Any pointers? I'm new to both python and django as well, but how about writing a presentation method and mention that method in the list_display of the admin interface, something like... class Timecard(models.Model): employee... ... notes... def get_week_result(self): # make a result string like '0.0 4.5 8.0 8.0 ...etc. # The 'start' field contains the weekday. # or maybe one method for each day of the week. return result class TimecardAdmin(admin.ModelAdmin): ... list_display = ('employee', ..., 'get_week_result') ... However, the model you defined is slightly askew from your presentation, if you want to look at a week at a time, maybe you'd be better of defining the model/record as being a week. Simply make things easier on yourself. Just my 2c /Lars Stavholm --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Displaying rows as columns
On 5/1/2009 5:47 PM, mamco wrote: > Hi, > 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 > > Being new to Python and Django, I can't seem to get a function written > to kick this output out. Any pointers? How would you approach this if you weren't using Python and Django? Is your inclination to do this using SQL or would you use an associative array of some sort in the programming language of your choice? I guess what I'm trying to say is this really doesn't have much to do with Python/Django, but is a general data pivot problem. So my advice would be to search for a solution that fits your need. It doesn't matter what the language/framework the solution is in, it's the general pattern you're after. A hint: I personally would avoid trying to do this in SQL, if nothing for the readability of the code. -- 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 -~--~~~~--~~--~--~---
Displaying rows as columns
Hi, 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 Being new to Python and Django, I can't seem to get a function written to kick this output out. Any pointers? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---