thank you joey, but i want to build search for queries like
1. restaurants in city1 2. city1 restaurants 3. Chinese restaurants 4. Chinese restaurants in city1 5. vegetarian restaurants in city1 6. vegetarian american restaurants the above are exact search queries we get from template. i wrote restas = restaurant.objects.filter(Q(name__contains = request.REQUEST.get('title')) & ~Q(city__name='') ) this works fine to search with restaurant name. i really have no idea on how to get results if the search phrase is complex. With Best -Ashwin. +91-9959166266 On Sun, Feb 3, 2013 at 12:46 AM, Joey Espinosa <jlouis.espin...@gmail.com>wrote: > I'll assume city1 is a City object: > > 1) restaurants.objects.filter(city=city1) > > 2) city1.restaurant_set.all() > > 3) For this one, you have your choices variable in a difficult setup. I'd > rewrite your restaurant class like this: > > class Restaurant(models.Model): # keep class names singular, with minor > exceptions > CHINESE = 'c' > AMERICAN = 'a' > JAPANESE = 'j' > > RESTAURANT_TYPES = ( > (CHINESE, 'Chinese'), > (AMERICAN, 'American'), > (JAPANESE, 'Japanese'), > ( > > name = models.CharField(max_length=456) > city = models.ForeignKey(city) # on a side note, the "city" class > should be defined as "class City(models.Model)" > rtype = models.CharField(max_length=10, choices=RESTAURANT_TYPES) # > another side note... don't use Python reserved keywords such as > > # > "type" for your own variables > is_veg = models.BooleanField(default=True) > > Now you'll be able to do this: > > Restaurant.objects.filter(rtype=Restaurant.CHINESE) > > 4) Using the above rewritten class as a guide: > > city1.restaurant_set.filter(rtype=Restaurant.CHINESE) > > or > > Restaurant.objects.filter(city=city1, rtype=Restaurant.CHINESE) > > 5) city1.restaurant_set.filter(is_veg=True) > > 6) city1.restaurant_set.filter(is_veg=True, rtype=Restaurant.AMERICAN) > > 7) ... and so on. > > Hope this helped. Also, I'd recommend getting familiar with this: > http://www.python.org/dev/peps/pep-0008/ > > -- > Joey "JoeLinux" Espinosa* > * > <http://therealjoelinux.blogspot.com> > <http://twitter.com/therealjoelinux><http://about.me/joelinux> > > > On Sat, Feb 2, 2013 at 1:13 PM, Aswani Kumar <aswin.1...@gmail.com> wrote: > >> hi all, i have a big question. >> how can i perform search queries like on following models >> >> RESTAURANT_TYPES = ( >> ('C', 'Chinese'), >> ('A', 'American'), >> ('J', 'Japanese'), >> ) >> >> class city (models.Model): >> name = models.CharField(max_length=200) >> >> class restaurants(models.Model): >> name = models.CharField(max_length=456) >> city = models.ForeignKey(city) >> type = models.CharField(max_length=10, choices=RESTAURANT_TYPES) >> is_veg = models.BooleanField(default=True) >> >> search queries: >> >> 1. restaurants in city1 >> 2. city1 restaurants >> 3. Chinese restaurants >> 4. Chinese restaurants in city1 >> 5. vegetarian restaurants in city1 >> 6. vegetarian american restaurants >> 7. and so on... >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com. >> To post to this group, send email to django-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.