我有一个 postgres 表,其中包含列id、name和item。counter现在我想将用户所属的那些行的counter值增加1 。
我尝试做类似的事情,但出现语法错误
UPDATE Items set counter = counter + 1 WHERE users = 'Tony';
Run Code Online (Sandbox Code Playgroud)
这应该将用户Tony所属的所有行counter的值增加1。类似于
最初的
id | name | item | counter
1 | Tony | car | 1
2 | Tony | bike | 1
3 | Ray | car | 1
4 | Ray | bike | 1
5 | Tony | cycle | 1
Run Code Online (Sandbox Code Playgroud)
最终的
id …Run Code Online (Sandbox Code Playgroud) 我有一个zip用户上传文件时收到的文件。本质zip上包含一个json我想要读取和处理的文件,而不必先创建该zip文件,然后解压缩它,然后读取内部文件的内容。
目前我只有较长的过程,如下所示
import json
import zipfile
@csrf_exempt
def get_zip(request):
try:
if request.method == "POST":
try:
client_file = request.FILES['file']
file_path = "/some/path/"
# first dump the zip file to a directory
with open(file_path + '%s' % client_file.name, 'wb+') as dest:
for chunk in client_file.chunks():
dest.write(chunk)
# unzip the zip file to the same directory
with zipfile.ZipFile(file_path + client_file.name, 'r') as zip_ref:
zip_ref.extractall(file_path)
# at this point we get a json file from the …Run Code Online (Sandbox Code Playgroud) 我有一些如下所示的数据
data1 = [[(271.760309837,)], [(289.247745329,)]]
data2 = [(u'A', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 2.66666666666667), (u'B', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 3.66666666666667), (u'C', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 12.25), (u'D', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 5.875), (u'E', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 9.06451612903226), (u'F', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 5.363636363636), (u'G', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 3.5), (u'H', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 9.53125), (u'I', …Run Code Online (Sandbox Code Playgroud) 我有一个纯粹用 设计的开关图标CSS。我从此页面https://www.w3schools.com/howto/howto_css_switch.asp获得了交换机的设计,并从堆栈溢出中的这个答案对交换机进行了一些添加的修改/sf/answers/2789262241/。
这是到目前为止的样子
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style type="text/css">
.switch {
position: relative;
display: inline-block;
width: 90px;
height: 34px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider …Run Code Online (Sandbox Code Playgroud)我有一张桌子叫user_info. 我想获取所有用户的姓名。所以该表有一个名为name. 所以在 sql 我做类似的事情
SELECT distinct(name) from user_info
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何在 django 中做同样的事情。通常,如果我已经知道某些值,那么我可以执行以下操作。
user_info.objects.filter(name='Alex')
Run Code Online (Sandbox Code Playgroud)
然后获取该特定用户的信息。
但是在这种情况下,对于给定的表,我想像在 sql 中一样使用 django ORM 获取所有名称值。
这是我的 Django 模型
class user_info(models.Model):
name = models.CharField(max_length=255)
priority = models.CharField(max_length=1)
org = models.CharField(max_length=20)
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 Django 中做到这一点?
我有一个包含数字的字符串.我想检查字符串是否包含0-9之间的所有数字.目前,我检查的方式非常慢,对于大字符串肯定没用.这是我的代码如下:
import sys
# check if all numbers (0-9) exist in a string
num = "31586055033755830765"
for i in num:
if int(i) not in [0, 1, 2 ,3 ,4 ,5 ,6, 7, 8, 9]:
print("The string doesn't have all the numbers")
sys.exit(1)
Run Code Online (Sandbox Code Playgroud)
代码工作正常但速度很慢.有没有更快的方法来完成任务?
python ×4
django ×2
css ×1
file ×1
html ×1
in-memory ×1
javascript ×1
jquery ×1
numpy ×1
pandas ×1
postgresql ×1
python-3.x ×1
sql ×1
zip ×1