Re: How to concatenate a list of Q objects?
Just to explain q = q | f if q else f is using the short version of if/else, so let's expand that: if q: q = q | f else: q = f q is only False when you have a Q object with no parameters i.e. Q() and as you may have seen above | means OR. This is because the Q object implements the __or__ special method. On 7 Apr, 19:56, Danielwrote: > Thanks alot guys. If I can be honest, I'm having a little trouble > digesting just this one line: > > q = q | f if q else f > > That line of code only allows (q1 | q2 | q3), right? > > It will not allow mixing "AND" as well as "OR" (i.e., q1 & q2 | q3)? > > Thanks! > > On Apr 7, 1:26 pm, Vinicius Mendes wrote: > > > On Wed, Apr 7, 2010 at 2:00 PM, Tom Evans wrote: > > > On Wed, Apr 7, 2010 at 5:39 PM, Daniel wrote: > > > > Hi, > > > > > Thank you for your help everyone. I know that I need to learn python > > > > better, and I did read those articles. What is still a bit unclear to > > > > me, though, is how could I add an "OR" or "AND" separator between Q > > > > objects? > > > > > So I have a list of qobjects like [qObj1, qObj2, qObj3]. > > > > > What I want is something like Sample.objects.filter((qObj1 | qObj2), > > > > qObj3) > > > > > I know that the default is for all Q objects to be "ANDed" together. > > > > I think the join operation is not going to work here, nor is > > > > concatenation, but is there something obvious that I'm missing? > > > > > THANK YOU :> > > > > Documentation on how to combine Q objects: > > > >http://docs.djangoproject.com/en/1.1/topics/db/queries/#complex-looku... > > > > So you want to loop through them, and 'or' them together.. > > > > filters = [ q1, q2, q3, q4, q5 ] > > > q = None > > > for f in filters: > > > q = q | f if q else f > > > Foo.objects.filter(q) > > > Refining a little: > > > filters = [q1,q2,q3,q4,q5] > > q = Q() > > for f in filters: > > q |= f > > Foo.objects.filter(q) > > > Q() is identity for & and |. > > > > Tom > > > > -- > > > You received this message because you are subscribed to the Google Groups > > > "Django users" group. > > > To post to this group, send email to django-us...@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. > > > __ > > Vinícius Mendes > > Solucione Sistemashttp://solucione.info/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: Custom QuerySet method that has an instance.save() error
Ok, I definitely did this before posting, but deleting all .pyc files in the django path (again) did fix it this time (as suggested somewhere). How confusing is pdb to show you a .py source code file, with no apparent problem, when the mismatch occurred in a .pyc for a previous and different .py file? On Apr 6, 5:53 pm, Tom M <mediaf...@googlemail.com> wrote: > Hi, > > I'm trying to use Model Managers to write some per model > functionality. I have a Book model that needs to be able to convert > some of it's members into SoldBook items. > > I want to be able to do: > Book.objects.filter(supplier='Jones').convert_sold() > > I'm basing my code > onhttp://stackoverflow.com/questions/2163151?tab=votes#tab-top > but my model looks like this: > > from custom_queryset import CustomQuerySetManager #see link above > from django.db.models.query import QuerySet > > class Book(models.Model): > objects = CustomQuerySetManager() > > class QuerySet(QuerySet): > def convert_sold(self): > for book in self.all(): > sb = SoldBook() > sb.title = book.title > #etc > sb.save() > book.delete() > > but when I call it with > Book.objects.filter(supplier='Jones').convert_sold() > > File "/home/user/webapps/django/lib/python2.5/django/db/models/sql/ > compiler.py", line 843, in as_sql > placeholder = field.get_placeholder(val, self.connection) > TypeError: get_placeholder() takes exactly 2 arguments (3 given) > > Unfortunately I'm using django trunk revision 11728 (5 months old?) > because webfaction haven't fixed their GIS database support for more > recent djangos :-( > > Any idea how to fix this would be greatly appreciated. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Custom QuerySet method that has an instance.save() error
Hi, I'm trying to use Model Managers to write some per model functionality. I have a Book model that needs to be able to convert some of it's members into SoldBook items. I want to be able to do: Book.objects.filter(supplier='Jones').convert_sold() I'm basing my code on http://stackoverflow.com/questions/2163151?tab=votes#tab-top but my model looks like this: from custom_queryset import CustomQuerySetManager #see link above from django.db.models.query import QuerySet class Book(models.Model): objects = CustomQuerySetManager() class QuerySet(QuerySet): def convert_sold(self): for book in self.all(): sb = SoldBook() sb.title = book.title #etc sb.save() book.delete() but when I call it with Book.objects.filter(supplier='Jones').convert_sold() File "/home/user/webapps/django/lib/python2.5/django/db/models/sql/ compiler.py", line 843, in as_sql placeholder = field.get_placeholder(val, self.connection) TypeError: get_placeholder() takes exactly 2 arguments (3 given) Unfortunately I'm using django trunk revision 11728 (5 months old?) because webfaction haven't fixed their GIS database support for more recent djangos :-( Any idea how to fix this would be greatly appreciated. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.