Changes between Initial Version and Version 6 of Ticket #3587
- Timestamp:
- Apr 21, 2007, 9:06:43 AM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #3587 – Description
initial v6 3 3 This is to document where I had problems getting filter() and order_by() to do what I want. I'm using MySQL. 4 4 Excerpts from my model : 5 {{{ 6 #!python 5 7 class Cadet(models.Model): 6 8 sqn = models.ForeignKey(Squadron) … … 18 20 camp = models.ForeignKey(Camp, core=True) 19 21 training_level = models.PositiveIntegerField("current training level") 22 }}} 20 23 21 24 The only places I can find where I couldn't get filtering to work are where I'm doing something that is probably too complex to do with SQL (although I'm no SQL expert). … … 23 26 Here's the function I ended up with after trying to get it going with 24 27 filter() : 28 29 {{{ 30 #!python 31 25 32 def exception_age(request, year_filter): 26 33 """Lists cadets that might not be the right age for the course they applied for.""" … … 31 38 apps = [a for a in apps if (a.cadet.age_on(this_june) < 32 39 float(a.camp.min_age)) or (a.cadet.ages_out() < this_sept)] 40 }}} 33 41 34 42 I don't have the filter() versions that I tried along the way, but it was based on comparing the application.cadet.date_of_birth field. 35 43 36 44 This one involves comparing the values in two tables : 45 {{{ 46 #!python 47 37 48 def exception_eligibility(request, year_filter): 38 49 """Lists cadets not in the right level for the course they applied for.""" … … 44 55 return render_to_response('camps/exception_eligibility.html', 45 56 {'object_list' : apps,}) 57 }}} 46 58 47 59 and this one requires doing a calculation on the difference between two fields (cadet.date_of_birth and cadet.enrollment_date) : 60 61 {{{ 62 #!python 63 48 64 def exception_enrollment(request, year_filter): 49 65 """Lists cadets enrolled too young who've applied this year.""" … … 55 71 return render_to_response('camps/exception_enrollment.html', 56 72 {'object_list' : cdts,}) 73 }}} 57 74 58 75 I do have examples of where order_by doesn't work. Here's one : 76 77 {{{ 78 #!python 79 59 80 def wing(app): 60 81 return app.cadet.sqn.wing.name … … 69 90 adet.sqn.wing.name') 70 91 [...] 92 }}} 71 93 72 94 You can see where I've commented-out what I wanted to do. The commented-out version gives : 95 {{{ 73 96 Exception Type: OperationalError 74 97 Exception Value: (1054, "Unknown column 75 98 'camps_application__cadet.sqn.wing.name' in 'order clause'") 99 }}} 76 100 77 101 This is following a series of foreign keys (application belongs to cadet, belongs to sqn, belongs to wing).