Hi Everybody!
I floated this at #DJUGL last week, but I thought I would just open the
question up to greater scrutiny; I have an iterative, slow solution which
works, but I would love to solve the problem more efficiently.
Rather than bury you under details, here's some pseudocode which illustrates
the challenge; we have three models:
class Ingredient(models.Model):
"""ingredient in a meal"""
name = models.CharField()
supplier = models.CharField()
price_per_kg = models.PositiveIntegerField()
calories_per_kg = models.PositiveIntegerField()
yadda = models.YaddaField()
class Recipe(models.Model):
"""cooking recipes"""
name = models.CharField()
ingredients = models.ManyToManyField(Ingredient)
minutes_to_prepare = models.PositiveIntegerField()
cooking_procedure = models.CharField()
class Child(models.Model):
"""those picky people who will be eating"""
name = models.CharField()
wants_ingredients = models.ManyToManyField(Ingredient)
hates_ingredients = models.ManyToManyField(Ingredient)
# CHALLENGE:
# for a given Child instance:
# alice = Child.objects.get(name="Alice")
#
# SELECT ALL Recipes WHERE:
# AT LEAST ONE OF recipe.ingredients IS AMONGST alice.wants_ingredients
# AND NONE OF recipe.ingredients ARE AMONGST alice.hates_ingredients
...without resorting to non-portable SQL, or ideally without custom SQL at all.
The *actual* problem is even more complex that this, but what really vexes me
is how to efficiently do a "ManyToManyField-intersects-some-predetermined-set"
search.
I believe I am thinking about the problem too hard and in the wrong way / need
to be querying the M2M relationship tables, but I can't step back far enough to
see the right answer.
Can anyone help me out of this rut, please?
Thanks in advance!
- alec
--
[email protected]
http://www.crypticide.com/dropsafe/
--
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.