Monday, May 9, 2011

Re: boolwan field filter

A bit off topic, but I can't resist. Does a college really always have
4 "branches"?
(I might be mistaken, but a branch sounds to me like a dynamic
property.)

I would consider using a many-to-many relation between colleges and
branches. It would change your question a lot...

class Branch(models.Model):
name = models.CharField(max_length=63)

class College(models.Model):
name = models.CharField(max_length=250)
city = models.ForeignKey(City)
branches = models.ManyToManyField(Branch)

I do not know of hand how to use an OR query on the join, when several
branches have been selected...

Given the assumption that a city has only a few colleges, I would
probably do something like this:
def search(request):
... form validation ...
city = City.objects.get(pk=form.cleaned_data['city_id']) # id
from a drop-down field
branches = form.cleaned_data['branch_ids']
# e.g. branches = [1, 45] id's coming from multiple choice,
multiple value check boxes
colleges = []
for college in city.colleges:
# city.colleges was implicitly created from the ForeignKey
definition in College
for branch in college.branches:
if branch.id in branches:
colleges.append(college)
break # next college
return render_to_response('search_results.html', {'colleges':
colleges})

See also:
http://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships
http://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view

Good luck! RR

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate