Changes between Initial Version and Version 6 of Ticket #3587


Ignore:
Timestamp:
Apr 21, 2007, 9:06:43 AM (17 years ago)
Author:
Malcolm Tredinnick
Comment:

Fixed formatting in summary.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #3587 – Description

    initial v6  
    33This is to document where I had problems getting filter() and order_by() to do what I want. I'm using MySQL.
    44Excerpts from my model :
     5{{{
     6#!python
    57class Cadet(models.Model):
    68    sqn = models.ForeignKey(Squadron)
     
    1820    camp = models.ForeignKey(Camp, core=True)
    1921    training_level = models.PositiveIntegerField("current training level")
     22}}}
    2023
    2124The 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).
     
    2326Here's the function I ended up with after trying to get it going with
    2427filter() :
     28
     29{{{
     30#!python
     31
    2532def exception_age(request, year_filter):
    2633    """Lists cadets that might not be the right age for the course they applied for."""
     
    3138    apps = [a for a in apps if (a.cadet.age_on(this_june) <
    3239float(a.camp.min_age)) or (a.cadet.ages_out() < this_sept)]
     40}}}
    3341
    3442I 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.
    3543
    3644This one involves comparing the values in two tables :
     45{{{
     46#!python
     47
    3748def exception_eligibility(request, year_filter):
    3849    """Lists cadets not in the right level for the course they applied for."""
     
    4455    return render_to_response('camps/exception_eligibility.html',
    4556{'object_list' : apps,})
     57}}}
    4658
    4759and 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
    4864def exception_enrollment(request, year_filter):
    4965    """Lists cadets enrolled too young who've applied this year."""
     
    5571    return render_to_response('camps/exception_enrollment.html',
    5672{'object_list' : cdts,})
     73}}}
    5774
    5875I do  have examples of where order_by doesn't work. Here's one :
     76
     77{{{
     78#!python
     79
    5980def wing(app):
    6081    return app.cadet.sqn.wing.name
     
    6990adet.sqn.wing.name')
    7091    [...]
     92}}}
    7193
    7294You can see where I've commented-out what I wanted to do. The commented-out version gives :
     95{{{
    7396Exception Type:         OperationalError
    7497Exception Value:        (1054, "Unknown column
    7598'camps_application__cadet.sqn.wing.name' in 'order clause'")
     99}}}
    76100
    77101This is following a series of foreign keys (application belongs to cadet, belongs to sqn, belongs to wing).
Back to Top