Query parameters for receiving post

39 views
Skip to first unread message

Hélio Miranda

unread,
Oct 8, 2013, 2:42:07 PM10/8/13
to django...@googlegroups.com
Hi
Here I am having a problem which is as follows:
I 'm getting parameters via post to make querys depending on paramtros I are passed ... doing gender filters.
So I'm doing well
def filter(request):
    if request.method == 'POST':
        namepost = request.POST.get('name')
        surnamepost = request.POST.get('surname')

        if namepost != None and surnamepost != None:
            result = [a.get_json() for a in Player.objects.filter((Q(name=namepost) & Q(surname=surnamepost)))]
       elif namepost != None and surnamepost == None:
            result = [a.get_json() for a in Player.objects.filter(name=namepost)]
        
        data = {"meta": {"total_count":len(result)}, "objects": result}
        aa = json.dumps(data)
        return HttpResponse(aa, content_type='application/json')

Is working properly , the problem is :
If it were only these two parameters , there was no problem , was to complete the rest of the hypotheses ... the problem is that I can have up to 10 parameters passed by post to the filters , and I never know which are passed , can be 1 , 2, 4, ... as the user makes the filter ...

But doing so would take a lot of ifs , it would have many chances ...
is there any way I can do this another way ?
Someone can help me ?

Ramiro Morales

unread,
Oct 8, 2013, 6:15:09 PM10/8/13
to django...@googlegroups.com
I can't thes this now but hopefully something similar to thois could
be of help: https://dpaste.de/FTnt

--
Ramiro Morales
@ramiromorales
Message has been deleted

Denis Chernoshchekov

unread,
Oct 9, 2013, 8:43:58 AM10/9/13
to django...@googlegroups.com

You can pass filter params like a dict - Player.objects.filter(**your_dict), so you need only prepare your dict from POST.

8 жовт. 2013 17:42, користувач "Hélio Miranda" <heli...@gmail.com> написав:
--
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 django...@googlegroups.com.
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/fc851085-d5bb-4567-b830-d25cdd66e499%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Hélio Miranda

unread,
Oct 9, 2013, 8:45:40 AM10/9/13
to django...@googlegroups.com
hi, thanks that helped a lot.
But I have a problem which is, for example when I do a filter by position, is not direct as other fields like name or surname. For the position I have to type this:
Player.objects.filter(position__in=Position.objects.filter(name=positionpost))
How can I implement this in code?
Can you help me?

Denis Chernoshchekov

unread,
Oct 9, 2013, 8:55:20 AM10/9/13
to django...@googlegroups.com

For position you may have dict like - **{' position__in ': Position.objects. filter(name=positionpost) }

9 жовт. 2013 11:45, користувач "Hélio Miranda" <heli...@gmail.com> написав:
--
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 django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Hélio Miranda

unread,
Oct 9, 2013, 9:07:48 AM10/9/13
to django...@googlegroups.com
I have code like this:

Just do not know how to put this part in the code ...
Where do I put this part?

Denis Chernoshchekov

unread,
Oct 9, 2013, 9:19:06 AM10/9/13
to django...@googlegroups.com


2013/10/9 Hélio Miranda <heli...@gmail.com>

--
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 django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Hélio Miranda

unread,
Oct 9, 2013, 9:45:56 AM10/9/13
to django...@googlegroups.com
yes, that was it ...
Just one more thing, if I would like to create the nationality of the player, so that the filter is:
Player.objects.filter(country__in=Country.objects.filter(nationality__in=Nationality.objects.filter(name='Espanhola')))

where only the step nationality, as I do, not only gives an if?

Denis Chernoshchekov

unread,
Oct 9, 2013, 10:05:27 AM10/9/13
to django...@googlegroups.com
Try this - Player.objects.filter(country__in=Nationality.objects.filter(name='Espanhola').distinct('country').values('country'))


2013/10/9 Hélio Miranda <heli...@gmail.com>

--
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 django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Hélio Miranda

unread,
Oct 9, 2013, 10:09:09 AM10/9/13
to django...@googlegroups.com
I put that work, I wanted to know was how to put in the code, because you can not do the same to position

Hélio Miranda

unread,
Oct 9, 2013, 10:14:28 AM10/9/13
to django...@googlegroups.com
was trying something like this:
if fld_name == 'nationality':
                    fld_name = 'nationality_in'
                    value = Player.objects.filter(country__in=Country.objects.filter(nationality__in=Nationality.objects.filter(name=value)))

But it does not work

Denis Chernoshchekov

unread,
Oct 9, 2013, 10:17:00 AM10/9/13
to django...@googlegroups.com
Sorry, i don't understand you... You can control all your values for all fields, you can generate dict which you like.


2013/10/9 Hélio Miranda <heli...@gmail.com>
I put that work, I wanted to know was how to put in the code, because you can not do the same to position

--
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 django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Denis Chernoshchekov

unread,
Oct 9, 2013, 10:18:09 AM10/9/13
to django...@googlegroups.com
fld_name = 'nationality_in' must be with double '_' fld_name = 'nationality__in'


2013/10/9 Denis Chernoshchekov <den.chern...@gmail.com>

Denis Chernoshchekov

unread,
Oct 9, 2013, 10:18:54 AM10/9/13
to django...@googlegroups.com
ok, i understand, i minute.


2013/10/9 Denis Chernoshchekov <den.chern...@gmail.com>

Denis Chernoshchekov

unread,
Oct 9, 2013, 10:22:09 AM10/9/13
to django...@googlegroups.com


2013/10/9 Denis Chernoshchekov <den.chern...@gmail.com>

Hélio Miranda

unread,
Oct 9, 2013, 10:25:56 AM10/9/13
to django...@googlegroups.com
gives error:
'BaseList' object has no attribute 'values​​'

The query has to be the same as I stated it works:
Player.objects.filter(country__in=Country.objects.filter(nationality__in=Nationality.objects.filter(name='Espanhola')))

Denis Chernoshchekov

unread,
Oct 9, 2013, 11:06:34 AM10/9/13
to django...@googlegroups.com
Your right... I can't testing now. 


2013/10/9 Hélio Miranda <heli...@gmail.com>

--
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 django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages