Remember that |= is just a shorthand for query = query | ... rest of parameters
So in the end it is just an "or" like you say, nothing magical about that Glad I could help Regards! On Thu, Nov 27, 2014 at 4:56 AM, <[email protected]> wrote: > Hi Vijay, > > thank you very much. I have not known, that |= also could be used on Q(). > Thought Q is only there for making "or" "not" and so stuff of calls! Great! > > Best Regards, > Mike > > *Gesendet:* Mittwoch, 26. November 2014 um 20:30 Uhr > *Von:* "Vijay Khemlani" <[email protected]> > *An:* [email protected] > *Betreff:* Re: How to make database queries that “filters” results > according to tuples of values? > I think you could construct a Q object like this: > > from django.db.models import Q > > query = Q() > for preference in user.preferences.all(): > query |= Q(dish=preference.dish) & Q(ingredient=preference.ingredient) > > Meal.objects.filter(query) > > That returns the meals where their (ingredient, dish) combination match at > least one of the preferences of the user. > > On Wed, Nov 26, 2014 at 3:54 PM, <[email protected]> wrote: >> >> Hi, >> let’s assume I have the following model: >> >> *class Ingredient(models.Model):* >> * ingredient= models.CharField(max_length=255)* >> *class Dish(models.Model):* >> * BREAKFAST = 'bf'* >> * LUNCH = 'l'* >> * DINNER = 'D'* >> * DISH_CHOICES = (* >> * (BREAKFAST, 'Breakfast'),* >> * (LUNCH, 'Lunch'),* >> * (DINNER, 'Dinner'),* >> * )* >> * dish = >> models.CharField(max_length=32,choices=DISH_CHOICES,unique=True)* >> >> *class Preference(models.Model):* >> * ingredient= models.ForeignKey(Ingredient)* >> * dish = models.ForeignKey(Dish)* >> * date_created = models.DateTimeField(auto_now_add=True)* >> * date_updated = models.DateTimeField(auto_now=True, >> verbose_name='Date updated')* >> * owner = models.ForeignKey(User, related_name='preferences')* >> >> *class Meal(models.Model):* >> * name = models.CharField(max_length=255)* >> * ingredient= models.ManyToManyField(Ingredient)* >> * kalorins = models.IntegerField()* >> * thumbnail = models.URLField(blank=True)* >> * dish= models.ForeignKey(Dish)* >> >> I want to execute a database query in the view resulting all meals that >> would match the preference’s of a user. >> But something like: *result = >> Meal.objects.filter(ingredient__in=user.request.name.pfreferences) *will >> select all the meals witch are in the preference list of the user, >> regardless his choice of the “Dish Type”. So if a person only likes tomatos >> for breakfast, he also will get all meals with tomates for lunch and dinner. >> >> So I am looking for a way, which returns only the “*Meal*s” where the >> *ingredient*s and the *dish *BOTH match the *ingredient*s and the *dish *of >> the *user’s Preference.* >> I haven’t found anything in the django books I have or the documentation >> Thanks a lot. >> Best Regards, >> Mike >> >> >> >> >> -- >> 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 [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/trinity-f0d51c02-a559-4c2a-b1d1-e807598864ff-1417028087688%403capp-gmx-bs08 >> <https://groups.google.com/d/msgid/django-users/trinity-f0d51c02-a559-4c2a-b1d1-e807598864ff-1417028087688%403capp-gmx-bs08?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CALn3ei0t_uWYDK7wk9ems1M9ZV8vRuXbTVT9MSTynuxUGhYWiQ%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CALn3ei0t_uWYDK7wk9ems1M9ZV8vRuXbTVT9MSTynuxUGhYWiQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/trinity-22d1ed63-154c-4950-91e0-c53bf101119b-1417075012723%403capp-gmx-bs25 > <https://groups.google.com/d/msgid/django-users/trinity-22d1ed63-154c-4950-91e0-c53bf101119b-1417075012723%403capp-gmx-bs25?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei3z2TUA4M8_HTYvRreweVzKn%3DGgDK1kfchWt_KozfehLA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

