> class Bio(models.Model):
> HEIGHT_CHOICES = (
> ('A',"< 5' (< 152cm)")
[snip]
> ,('Z',"7' 0'' (213cm)")
> ,('*',"> 7'' (> 213cm)")
> )
> WEIGHT_CHOICES = (('A',"<100lbs (<45kg)"),('B',"100lbs (45kg)"),
[snip]
> ('R',">250lbs (>113kg)")
> )
[snip some more]
> models.CharField(maxlength=1,choices=WEIGHT_CHOICES,blank=True,default='')
> height =
> models.CharField(maxlength=1,choices=HEIGHT_CHOICES,blank=True,default='')
[snip yet more]
> 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)
[snip even more]
> 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'
I think what you're looking for would be something like
results = Bio.objects.all()
for fieldname, vbl_name in [
('race', 'chk_race'),
('height', 'chk_height'),
('eye_col', 'chk_eye_color'),
# add other field/chk pairs here
]:
if vbl_name in request.POST:
results = results.filter(**{
fieldname + '__in':
request.POST.getlist(vbl_name)
})
do_something(results)
You'd have to toy around with it a little to make sure it's doing
what you want, but it basically builds successive filter() calls,
using "<fieldname>__in = [<list_of_values>]" format calls, and
then uses keyword expansion (the "**{...}" notation) to expand
those into the call.
-tim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---