I'm developing my first Django website where the users should be able
to search other user profiles based on certain paramaters like race,
age, gender, height, weight, eye color, hair color, etc....

This is how my model looks:  (code have been shortened to only include
the pertinent sections)

class Bio(models.Model):
    HEIGHT_CHOICES = (
                      ('A',"< 5' (< 152cm)")
                      ,('B',"5' 0'' (152cm)")
                      ,('C',"5' 1'' (155cm)")
                      ,('D',"5' 2'' (157cm)")
                      ,('E',"5' 3'' (160cm)")
                      ,('F',"5' 4'' (163cm)")
                      ,('G',"5' 5'' (165cm)")
                      ,('H',"5' 6'' (168cm)")
                      ,('I',"5' 7'' (170cm)")
                      ,('J',"5' 8'' (173cm)")
                      ,('K',"5' 9'' (175cm)")
                      ,('L',"5' 10'' (178cm)")
                      ,('M',"5' 11'' (180cm)")
                      ,('N',"6' 0'' (183cm)")
                      ,('O',"6' 1'' (185cm)")
                      ,('P',"6' 2'' (188cm)")
                      ,('Q',"6' 3'' (191cm)")
                      ,('R',"6' 4'' (193cm)")
                      ,('S',"6' 5'' (196cm)")
                      ,('T',"6' 6'' (198cm)")
                      ,('U',"6' 7'' (201cm)")
                      ,('V',"6' 8'' (203cm)")
                      ,('W',"6' 9'' (206cm)")
                      ,('X',"6' 10'' (208cm)")
                      ,('Y',"6' 11'' (211cm)")
                      ,('Z',"7' 0'' (213cm)")
                      ,('*',"> 7'' (> 213cm)")
                      )
    WEIGHT_CHOICES = (('A',"<100lbs (<45kg)"),('B',"100lbs (45kg)"),
('C',"110lbs (50kg)"),('D',"120lbs (54kg)"),('E',"130lbs (59kg)"),
('F',"140lbs (64kg)"),('G',"150lbs (68kg)"),('H',"160lbs (73kg)"),
('I',"170lbs (77kg)"),('J',"180lbs (82kg)"),('K',"190lbs (86kg)"),
('L',"200lbs (91kg)"),('M',"210lbs (95kg)"),('N',"220lbs (100kg)"),
('O',"230lbs (104kg)"),('P',"240lbs (109kg)"),('Q',"250lbs (113kg)"),
('R',">250lbs (>113kg)")
                      )
    RACE_CHOICES = (('A','Asian'),('B','Black'),('W','Caucasian or
White'),('H','Hispanic'),('E','Middle Eastener'),('M','Mixed'),
('I','Native American'),('O','Other'))
    EYE_COLOR_CHOICES = (('L','Black'),('W','Brown'),('B','Blue'),
('G','Green'),('H','Hazel'))
    HAIR_COLOR_CHOICES = (('L','Black'),('W','Brown'),('C','Balding'),
('G','Gray'),('B','Blond'))

    user = models.ForeignKey(User)
    race =
models.CharField(maxlength=1,choices=RACE_CHOICES,blank=True,default='')
    weight =
models.CharField(maxlength=1,choices=WEIGHT_CHOICES,blank=True,default='')
    height =
models.CharField(maxlength=1,choices=HEIGHT_CHOICES,blank=True,default='')
    eye_color =
models.CharField(maxlength=1,choices=EYE_COLOR_CHOICES,blank=True,default='')
    hair_color =
models.CharField(maxlength=1,choices=HAIR_COLOR_CHOICES,blank=True,default='')

In my html form I have checkboxes for each of the field options, like:

<input type="checkbox" id="id_eye_color" name="chk_eye_color"
value="L">Black
<input type="checkbox" id="id_eye_color" name="chk_eye_color"
value="W">Brown
<input type="checkbox" id="id_eye_color" name="chk_eye_color"
value="B">Blue
<input type="checkbox" id="id_eye_color" name="chk_eye_color"
value="G">Green
<input type="checkbox" id="id_eye_color" name="chk_eye_color"
value="H">Hazel

The following view code collects the checkbox values and creates a
dynamic sql string:

def add_param(request,chk_name,field_name):
    if request.POST.has_key(chk_name):
        ors = []
        for value in request.POST.getlist(chk_name):
            ors.append("%s='%s'" % (field_name,value))
        if len(ors):
            return '(' + join(ors,' OR ') + ')'
    return None

def profile_search_new(request):
        if request.method == 'POST':
            where = []

            qry = add_param(request,'chk_race','race')
            if qry:
                where.append(qry)

            qry = add_param(request,'chk_eye_color','eye_color')
            if qry:
                where.append(qry)

            qry = add_param(request,'chk_weight','weight')
            if qry:
                where.append(qry)

            qry = add_param(request,'chk_height','height')
            if qry:
                where.append(qry)

            qry = add_param(request,'chk_hair_color','hair_color')
            if qry:
                where.append(qry)

            if len(where):
                sql = "SELECT user_id FROM Bio WHERE " + join(where,'
AND ')
                rows = sql_query(sql)

Depending on the checkbox selection it might produce a sql string
like:
SELECT user_id FROM Bio WHERE (race='A' OR race='W' OR race='H') AND
(eye_color='B' OR eye_color='G' OR eye_color='H') AND (hair_color='W'
OR hair_color='B')

I would like to do this in a more 'Django way' without having to
resort to executing a direct sql string query.
I'm not sure how to build this 'filter' or if I should be using 'Q'
objects.

Hope the above code conveys my intention, let me know if I need to be
more specific.

Any advice is greatly appreciated!
Karl ;)


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to