On Thu, 2009-04-23 at 11:00 +0100, Andrew Ingram wrote: > Hi All, > > I'm looking for an efficient way to solve the following problem, I'm > happy to use custom sql if necessary. > > Basically I have an Order model and an OrderItem model (ecommerce > site), structured as follows: > > Order(models.Model): > ... some fields ... > > OrderItem(models.Model): > ... some fields ... > order = models.ForeignKey(Order,related_name='items') > progress = models.CharField(max_length=20) > > The site logic dictates that an Order is considered 'Open', if at > least one item has a progress of 'PROCESSING', 'CHARGED' or > 'ACTION_NEEDED', if ALL the items are of any other progress value the > order is considered 'Closed'. > > I'm trying to figure out the QuerySet chain to run on the Order object > to find all the open orders, and another for all the closed orders. > > Naturally I started out looked at > Order.objects.exclude(items__progress__in=(list of closed progress > values)), but this doesn't work. I'm assuming because it will just > take the progress of the first item it finds rather than looking at > the whole set.
This should work. Your assumption isn't correct, by the way. The queryset will be all Order objects that do not contain *any* items matching those statements. Order.objects.exclude(...) is the exact complement of Order.objects.filter(...) if the same stuff is inside the "..." piece in each case. What are you seeing here? Can you give a small example of an Order and corresponding OrderStatus items where the Order is returned by this queryset when it shouldn't be? Using exclude() and the "__in" lookup type is the natural way to approach this, so I'd be interested in helping you work out why that isn't working for you. For debugging purposes, if you're happy reading SQL, you can look at what is being sent to the database by viewing Order.objects.exclude(...).query.as_sql() (that shows you the SQL and parameters that would be sent -- it is what Django uses to construct the SQL.) Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---