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/
On Sat, Feb 2, 2013 at 1:13 PM, Aswani Kumar <aswin.1231@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:
- restaurants in city1
- city1 restaurants
- Chinese restaurants
- Chinese restaurants in city1
- vegetarian restaurants in city1
- vegetarian american restaurants
- 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+unsubscribe@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+unsubscribe@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.
No comments:
Post a Comment