Monday, September 24, 2018

Re: Chaining Q objects

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: django...@googlegroups.com [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 djang...@googlegroups.com.
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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.

No comments:

Post a Comment