On Jul 29, 5:18 am, bweiss <weissmanbia...@gmail.com> wrote: > I've got this to work in python shell mode, but am having trouble > translating it into a django view, and am hoping someone can point out > where I'm stuffing up. > > For background, I'm working with a database with two models: > "Employees" and "Projects", joined by a ForeignKey field on the > Project model called "EmployeeName". Each project has exactly one > employee associated with it, and also has a field called "Type" with > options of 1, 2, or 3. > > I need to write a view that will generate a list of employees and > then, for each employee in that list, generate a related list of all > Type 1 projects under their name, a list of Type 2 projects under > their name, and a list of Type 3 projects under their name. > > (The reason I have to do it this way, rather than simply generating > one big list of all projects and then just re-ordering by name and > type, is that I want to be able to cycle through each list separately > in order to do some template-side manipulation of the way the data is > presented. Eg. control how many projects of each type per person are > presented, what to display if an employee has no projects of a certain > type, etc etc. I know how to do this with the template tags, but am > having trouble getting the template to access the data lists.) > > I've got the query to work in shell mode like so: > > >> for employee in Employee.objects.filter([filters here]): > >> a = Project.objects.filter(Type=1).filter(EmployeeName__id=employee.id) > >> b = Project.objects.filter(Type=2).filter(EmployeeName__id=employee.id) > >> c = Project.objects.filter(Type=3).filter(EmployeeName__id=employee.id) > >> print e.Name, a, b, c > > That works fine and displays exactly the dataset I want. However, I'm > having trouble translating that into the django view. > > At the moment, my view looks like this: > > def employeeprojectreport(request): > employee_list = Employee.objects.filter([filters here]) > for employee in employee_list: > a_list = Project.objects.filter(Type=1).filter > (EmployeeName__id=employee.id) > b_list = Project.objects.filter(Type=2).filter > (EmployeeName__id=employee.id) > c_list = Project.objects.filter(Type=3).filter > (EmployeeName__id=employee.id) > > t = loader.get_template('employeeprojectreport.html') > c = Context({ > 'employee_list': employee_list, > 'a_list': a_list, > 'b_list': b_list, > 'c_list': c_list, > > }) > return HttpResponse(t.render(c)) > > The template is cycling through employee_list the way I want it to, > but I can't seem to call the related lists through the template. > Either I'm defining things wrongly in the view, or doing something > wrong at the template end. > > In the template, I've tried: > {% for employee in employee_list %} > {{ a }} > {% endfor %} > > and: > {% for employee in employee_list %} > {% for a in a_list %} > {{ a }} > {% endfor %}{% endfor %} > > and: > {% for employee in employee_list %} > {% for a in employee.a_list %} > {{ a }} > {% endfor %}{% endfor %} > > The first outputs an empty set of square brackets [], the other two > don't output anything. > > Does anyone have any suggestions as to where I might be going wrong? > > Many thanks, > > Bianca
Your view isn't defining one of each list per employee - it's defining a single a_list, b_list and c_list, and overwriting it each time. So, by the time you reach the end of the employee list, your three lists just contain the data for the final employee. One way of fixing this is to assign each list as an attribute of the relevant employee: for employee in employee_list: employee.a_list = Project.objects.filter(Type=1).filter( EmployeeName__id=employee.id) etc. Then in your templates you can do {% for a in employee.a_list %} etc. However, I have to say that this is an extremely inefficient method of doing the lookups - you're generating three database calls per employee. If you have hundreds of employees you'll soon run into performance issues. You may want to investigate the aggregation features in version 1.1 - that will allow you to group your projects by project type, which will probably be more efficient. -- 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---