0

I am new to python and Django. I haven't found anything in documentations so I have to write here.

I have such problem.

I have cars table where you can find it's make, model. year and so on. Usually i make request by just Cars.objects.filter(make=x, model=y, year=z)

I want to make search and all params are in array. There are many params and is it possible to make something like Cars.objects.filter(array)

Ok. How i am getting my data. It is regular post form which I send by ajax to my view class. I have list of allowed params which can be found in users request. I have different names for tableview name and input name, but i have everything i need in prepared array. It can take user's sent car-name Acura and make something like {'make':123}. At the end i have an array which is fully compatible with my db {'make': 123, 'model':321}. Can i make a request using this array and not write by hands every param?

2
  • have you made any search form and written its view yet? add it to your question, how you are getting params in array ? Commented Nov 26, 2017 at 13:57
  • 1
    you have an dict of search parameters, not an list, right? {'make': 123, 'model': 'abc'} is a python dict, not an array (which in python is called a list by the way). You know if you have a dict, let's say params, then passing **params in a function call will be the same as calling the function with each key of the dict set to its value: Cars.objects.filter(**params). Commented Nov 26, 2017 at 15:38

1 Answer 1

1

if arguments are stored in this format:

args = [('make', 'x'),('model', 'y'),('year', 'z')]

You can try:

arg_dict = dict(args)
Cars.objects.filter(**arg_dict)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.