如何摆脱“”从字符串到非字符串
我有这个
a = "'Something': False"
我想要它是这个
a = 'Something': False
我该怎么做?我可以去掉双引号内的内容,但不能去掉引号本身。研究过,没找到。我现在脑子有障碍。抱歉,
我正在尝试这样做:
query_results = UserProfile.objects.filter(Location: location, Gender: gender).extra(select={'rank': a > 0}, order_by=['-rank'])
位置 = 某地,性别 = 男性或女性。
但是当我不想要特定的性别或地点时该怎么办?我唯一能想到要做的就是做
Location__isnull:False, Gender__isnull:False
但我不能两者兼而有之,
Location:location, Location__isnull:False.
因此我必须将位置参数作为变量。
那我怎么能这样做呢。涉及位置和性别的信息来自请求。GET
我无法发布我的代码,因为我不断删除和更改意大利面条以尝试制作可食用的东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于您想要做的事情,最简单的方法是构建您想要提供的参数的字典,然后将它们传递给 filter() 调用。这是一些无法正常工作的示例代码,可能会让您朝着正确的方向前进。
从您的问题中不清楚您是否真的需要在这些记录中搜索不为空的记录。空的
.filter()
将返回所有记录,就像您调用.all()
一样,如果您使用我建议的模式,您可以省略所有 else 子句,只提供filter(**arguments)
一个空字典。换句话说,您只需要指定您真正需要的查询术语,并将它们添加到参数字典中即可。然后你用
**arguments
调用过滤器。**
是一种特殊的 Python 语法,它表示“获取此字典中的所有键/值对,并将它们转换为此函数的关键字参数”。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."你不能。这不是有效的Python语法:
'Something': False
...除非它在字典中:{'Something': False}
。不,您不能将'Something': False
分配给变量。You can't. This is not valid python syntax:
'Something': False
... unless it's inside a dictionary:{'Something': False}
. And no, you can not assign'Something': False
to a variable.