Django framework and complex DB request
By Maxime Biais, Saturday 6 January 2007 at 20:07 :: Django :: #4 :: rss
I'm using the Django framework for my project at work. Django is really great but sometimes I have difficulties to find the "good way". Today, I discovered how to do complex lookups. Starting with this models :
from django.db import models class Recipe: name = models.CharField(unique=True) class Ingredient: recipe = models.ForeignKey(Recipe) name = models.CharField() quantity = models.FloatField(max_digits=5, decimal_places=2)You're looking for all recipes containing ingredient name begining by chick or named turkey ?
from myapp.models import Recipe from django.db.models import Q Recipe.objects.filter(Q(ingredient__name__startswith="chick")\ |Q(ingredient__name="turkey"))Or maybe recipes with name containing "thanks giving" and using chicken as ingredient:
Recipe.objects.filter(Q(name__icontains="thanks giving")\ &Q(ingredient__name="chicken"))Read more in the Django documentation.




Comments
No comment.
Write your comment