Search code examples
pythonarraysdjangorequestmodels

Django model request array


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?


Solution

  • 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)