I have this
a = "'Something': False"
I want it to be this
a = 'Something': False
How do I do it? I can strip things within the double quotation marks, but not the quotation marks itself. Have researched, cant find. I have mind block at moment. Sorry
Im trying to do this:
query_results = UserProfile.objects.filter(Location: location, Gender: gender).extra(select={'rank': a > 0}, order_by=['-rank'])
where Location = Someplace and Gender = Male or Female.
But what about when i did not want a specific gender or location. The only thing i could think of to do was to do
Location__isnull:False, Gender__isnull:False
But i cant have both
Location:location, Location__isnull:False.
Thus i have to have the location argument as a variable.
How could i then do this. The information referring to Location and gender is coming from a request.GET
I cant post my code as i keep deleting and changing spaghetti to try make something edible.
For what you’re trying to do, the easiest way is to build up a dictionary of the arguments you want to supply, then pass them to the filter() call. Here’s some non-working sample code that might get you heading in the right direction.
It’s not clear from your question whether or not you really need to search these for records which are not null. An empty
.filter()would return all the records just as if you’d called.all(), and if you use the pattern I’ve suggested, you could omit all the else clauses and just feedfilter(**arguments)an empty dictionary.In other words, you only need to specify the query terms you really require, and do that by adding them to the arguments dictionary. Then you call filter with
**arguments. The**is a special Python syntax that says “take all the key/value pairs in this dictionary and turn them into keyword arguments for this function.”