Chaining Q objects

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Chaining Q objects

MikeKJ
Using this as an example from Dan Herrar
enter code here

        from django.db.models import Q
        user_pk = 1
        category_pk = 1  #some times None
        f = Q( user__pk = user_pk, date=now() )
        if category_pk is not None:
            f &= Q( category__pk = category_pk )
        todays_items = Item.objects.filter( f  )

I am constructing a massive filter chain (I'd rather not) so I have...
enter code here

        from django.db.models import Q
        f = Q(availability_text=this)
        if 'cabins' in request.POST:
            cabins = request.POST['cabins']
            if cabins != "0" and cabins != "":
                c = cabins
                c = int(c)
                f &= Q(cabins=c)
            if 'thruster' in request.POST:
                thruster = request.POST['thruster']
                if thruster != 0:
                    t = 1
                    f &= Q(thruster=t)

etc etc

this is the result of an f trial

(AND: ('availability_text', ''), ('bow_thruster', 1))

but I get

"Too many values to unpack" at

                wm = Boat.objects.filter(name=x).filter( f )[:1].get()

which is confusing as I am handling 4 lists and this is the 3rd iteration and it didn't error on iteration 1 or 2

Any insight would be gratefully received

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: Chaining Q objects

Matthew Pava

I’m not entirely sure if the error is from your Q chain, but something I would try is to replace “[:1].get()” with “.first()”.

wm = Boat.objects.filter(name=x).filter( f ).first()

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of MikeKJ
Sent: Wednesday, September 19, 2018 9:29 AM
To: Django users
Subject: Chaining Q objects

 

Using this as an example from Dan Herrar
enter code here

        from django.db.models import Q
        user_pk = 1
        category_pk = 1  #some times None
        f = Q( user__pk = user_pk, date=now() )
        if category_pk is not None:
            f &= Q( category__pk = category_pk )
        todays_items = Item.objects.filter( f  )

I am constructing a massive filter chain (I'd rather not) so I have...
enter code here

        from django.db.models import Q
        f = Q(availability_text=this)
        if 'cabins' in request.POST:
            cabins = request.POST['cabins']
            if cabins != "0" and cabins != "":
                c = cabins
                c = int(c)
                f &= Q(cabins=c)
            if 'thruster' in request.POST:
                thruster = request.POST['thruster']
                if thruster != 0:
                    t = 1
                    f &= Q(thruster=t)

etc etc

this is the result of an f trial

(AND: ('availability_text', ''), ('bow_thruster', 1))

but I get

"Too many values to unpack" at

                wm = Boat.objects.filter(name=x).filter( f )[:1].get()

which is confusing as I am handling 4 lists and this is the 3rd iteration and it didn't error on iteration 1 or 2

Any insight would be gratefully received

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com.
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/766474725af642169c5d6cf8b056bab9%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Chaining Q objects

Simon Charette
Hey Mike,

Please provide the full traceback.

Also, since all your conditions are ANDed you could use a dict to build filter() kwargs instead of Q() objects.

Cheers,
Simon

Le mercredi 19 septembre 2018 10:36:13 UTC-4, Matthew Pava a écrit :

I’m not entirely sure if the error is from your Q chain, but something I would try is to replace “[:1].get()” with “.first()”.

wm = Boat.objects.filter(name=x).filter( f ).first()

 

From: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@... [mailto:<a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@googlegroups.com] On Behalf Of MikeKJ
Sent: Wednesday, September 19, 2018 9:29 AM
To: Django users
Subject: Chaining Q objects

 

Using this as an example from Dan Herrar
enter code here

        from django.db.models import Q
        user_pk = 1
        category_pk = 1  #some times None
        f = Q( user__pk = user_pk, date=now() )
        if category_pk is not None:
            f &= Q( category__pk = category_pk )
        todays_items = Item.objects.filter( f  )

I am constructing a massive filter chain (I'd rather not) so I have...
enter code here

        from django.db.models import Q
        f = Q(availability_text=this)
        if 'cabins' in request.POST:
            cabins = request.POST['cabins']
            if cabins != "0" and cabins != "":
                c = cabins
                c = int(c)
                f &= Q(cabins=c)
            if 'thruster' in request.POST:
                thruster = request.POST['thruster']
                if thruster != 0:
                    t = 1
                    f &= Q(thruster=t)

etc etc

this is the result of an f trial

(AND: ('availability_text', ''), ('bow_thruster', 1))

but I get

"Too many values to unpack" at

                wm = Boat.objects.filter(name=x).filter( f )[:1].get()

which is confusing as I am handling 4 lists and this is the 3rd iteration and it didn't error on iteration 1 or 2

Any insight would be gratefully received

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;"> djang...@....
Visit this group at <a href="https://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;"> https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6e93145a-f448-4d8c-a8bc-64c532fcca4a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Chaining Q objects

MikeKJ
In reply to this post by Matthew Pava
Cheers Matthew, can't use .first() due to reskinning an existing site with an older version

On Wednesday, 19 September 2018 15:36:13 UTC+1, Matthew Pava wrote:

I’m not entirely sure if the error is from your Q chain, but something I would try is to replace “[:1].get()” with “.first()”.

wm = Boat.objects.filter(name=x).filter( f ).first()

 

From: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@... [mailto:<a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@googlegroups.com] On Behalf Of MikeKJ
Sent: Wednesday, September 19, 2018 9:29 AM
To: Django users
Subject: Chaining Q objects

 

Using this as an example from Dan Herrar
enter code here

        from django.db.models import Q
        user_pk = 1
        category_pk = 1  #some times None
        f = Q( user__pk = user_pk, date=now() )
        if category_pk is not None:
            f &= Q( category__pk = category_pk )
        todays_items = Item.objects.filter( f  )

I am constructing a massive filter chain (I'd rather not) so I have...
enter code here

        from django.db.models import Q
        f = Q(availability_text=this)
        if 'cabins' in request.POST:
            cabins = request.POST['cabins']
            if cabins != "0" and cabins != "":
                c = cabins
                c = int(c)
                f &= Q(cabins=c)
            if 'thruster' in request.POST:
                thruster = request.POST['thruster']
                if thruster != 0:
                    t = 1
                    f &= Q(thruster=t)

etc etc

this is the result of an f trial

(AND: ('availability_text', ''), ('bow_thruster', 1))

but I get

"Too many values to unpack" at

                wm = Boat.objects.filter(name=x).filter( f )[:1].get()

which is confusing as I am handling 4 lists and this is the 3rd iteration and it didn't error on iteration 1 or 2

Any insight would be gratefully received

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="cUeuj8liCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;"> djang...@....
Visit this group at <a href="https://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;"> https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4bcbf096-1b4c-4754-93ee-e214e446afee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Chaining Q objects

MikeKJ
In reply to this post by Simon Charette
many thanks Simon dict filter kwargs was definitely the way to go!

On Wednesday, 19 September 2018 15:45:07 UTC+1, Simon Charette wrote:
Hey Mike,

Please provide the full traceback.

Also, since all your conditions are ANDed you could use a dict to build filter() kwargs instead of Q() objects.

Cheers,
Simon

Le mercredi 19 septembre 2018 10:36:13 UTC-4, Matthew Pava a écrit :

I’m not entirely sure if the error is from your Q chain, but something I would try is to replace “[:1].get()” with “.first()”.

wm = Boat.objects.filter(name=x).filter( f ).first()

 

From: [hidden email] [mailto:django...@googlegroups.com] On Behalf Of MikeKJ
Sent: Wednesday, September 19, 2018 9:29 AM
To: Django users
Subject: Chaining Q objects

 

Using this as an example from Dan Herrar
enter code here

        from django.db.models import Q
        user_pk = 1
        category_pk = 1  #some times None
        f = Q( user__pk = user_pk, date=now() )
        if category_pk is not None:
            f &= Q( category__pk = category_pk )
        todays_items = Item.objects.filter( f  )

I am constructing a massive filter chain (I'd rather not) so I have...
enter code here

        from django.db.models import Q
        f = Q(availability_text=this)
        if 'cabins' in request.POST:
            cabins = request.POST['cabins']
            if cabins != "0" and cabins != "":
                c = cabins
                c = int(c)
                f &= Q(cabins=c)
            if 'thruster' in request.POST:
                thruster = request.POST['thruster']
                if thruster != 0:
                    t = 1
                    f &= Q(thruster=t)

etc etc

this is the result of an f trial

(AND: ('availability_text', ''), ('bow_thruster', 1))

but I get

"Too many values to unpack" at

                wm = Boat.objects.filter(name=x).filter( f )[:1].get()

which is confusing as I am handling 4 lists and this is the 3rd iteration and it didn't error on iteration 1 or 2

Any insight would be gratefully received

--
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 django-users...@googlegroups.com.
To post to this group, send email to [hidden email].
Visit this group at <a href="https://groups.google.com/group/django-users" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;"> https://groups.google.com/d/msgid/django-users/80b8d218-f948-4985-a825-f5d13af1f4ca%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7cbf8905-898a-4642-a6fb-1e2db9a80acd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.