如何使用Flask-SQLAlchemy删除单个表中的所有行?
寻找这样的东西:
>>> users = models.User.query.all()
>>> models.db.session.delete(users)
# but it errs out: UnmappedInstanceError: Class '__builtin__.list' is not mapped
Run Code Online (Sandbox Code Playgroud) 我可以在座位表中查询没有分配邀请的所有座位:
seats = Seat.query.filter_by(invite=None).all()
Run Code Online (Sandbox Code Playgroud)
但是,当查询所有分配了邀请的座位时,我得到一个错误:
seats = Seat.query.filter_by(invite!=None).all()
Run Code Online (Sandbox Code Playgroud)
这是我的Seat类:
NameError: name 'invite' is not defined
Run Code Online (Sandbox Code Playgroud)
如何查询所有者不为空的所有座位?
谢谢
在before_request()函数(下面)中,/login如果用户尚未登录,我想将用户重定向到.是否有一个特殊的变量可以为我提供当前的URL,如下例所示?
@app.before_request
def before_request():
# the variable current_url does not exist
# but i want something that works like it
if (not 'logged_in' in session) and (current_url != '/login'):
return redirect(url_for('login'))
Run Code Online (Sandbox Code Playgroud)
我需要检查当前的URL /login,因为如果我不这样做,服务器进入无限循环.
完成React教程后,这是我的index.html文件:
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.2/marked.min.js"></script>
</head>
<body>
<div id="content"></div>
<script src="lib/main.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的src/main.jsx文件:
var CommentBox = React.createClass({
getInitialState: function() {
return {data: []};
},
loadCommentsFromServer: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
handleCommentSubmit: function(comment) {
var comments = this.state.data;
var newComments = …Run Code Online (Sandbox Code Playgroud) 我想检查freemarker模板中的序列是否为空.
此代码段用于检查序列是否包含值:
<#if node.attachments?seq_contains("blue")>
<pre>hello</pre>
</#if>
Run Code Online (Sandbox Code Playgroud)
但是,如果node.attachments是空的我想做别的事情.
那是这个的语法?
我正在使用Jest和Enzyme来测试React复选框组件.
这是我的测试:
it('triggers checkbox onChange event', () => {
const configs = {
default: true,
label: 'My Label',
element: 'myElement',
}
const checkbox = shallow(
<CheckBox
configs={configs}
/>
)
checkbox.find('input').simulate('click')
})
Run Code Online (Sandbox Code Playgroud)
我在运行测试时遇到此错误:
TypeError: Cannot read property 'target' of undefined
Run Code Online (Sandbox Code Playgroud)
这是我的组件的输入:
<div className="toggle-btn sm">
<input
id={this.props.configs.element}
className="toggle-input round"
type="checkbox"
defaultChecked={ this.props.defaultChecked }
onClick={ e => this.onChange(e.target) }
>
</input>
</div>
Run Code Online (Sandbox Code Playgroud)
我想那我需要通过一个事件作为第二个对象simulate,但我不知道如何做到这一点.
谢谢
我在JavaScript中处理utf-8字符串并需要转义它们.
escape()/ unescape()和encodeURI()/ decodeURI()都可以在我的浏览器中使用.
逃逸()
> var hello = "?????"
> var hello_escaped = escape(hello)
> hello_escaped
"%uC548%uB155%uD558%uC138%uC694"
> var hello_unescaped = unescape(hello_escaped)
> hello_unescaped
"?????"
Run Code Online (Sandbox Code Playgroud)
是encodeURI()
> var hello = "?????"
> var hello_encoded = encodeURI(hello)
> hello_encoded
"%EC%95%88%EB%85%95%ED%95%98%EC%84%B8%EC%9A%94"
> var hello_decoded = decodeURI(hello_encoded)
> hello_decoded
"?????"
Run Code Online (Sandbox Code Playgroud)
虽然encodeURI()和decodeURI()使用上面的utf-8字符串,但docs(以及函数名称本身)告诉我这些方法适用于URI; 我没有看到任何地方提到的utf-8字符串.
简单地说,对于utf-8字符串使用encodeURI()和decodeURI()是否可以?
这是我的__tests__/App.js档案:
import React from 'react';
import ReactDOM from 'react-dom';
import App from '../src/containers/App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
// sanity check
it('one is one', () => {
expect(1).toEqual(1)
});
Run Code Online (Sandbox Code Playgroud)
这是我在运行时得到的输出yarn test:
FAIL __tests__/App.js
? renders without crashing
ReferenceError: document is not defined
at Object.<anonymous>.it (__tests__/App.js:6:15)
? renders without crashing (1ms)
? one is one
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total …Run Code Online (Sandbox Code Playgroud) 假设我有一个这样的模型:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
hometown = db.Column(db.String(140))
university = db.Column(db.String(140))
Run Code Online (Sandbox Code Playgroud)
要获取纽约的用户列表,这是我的查询:
User.query.filter_by(hometown='New York').all()
Run Code Online (Sandbox Code Playgroud)
要获取前往USC的用户列表,这是我的查询:
User.query.filter_by(university='USC').all()
Run Code Online (Sandbox Code Playgroud)
要获得纽约的用户列表,以及谁去USC,这是我的查询:
User.query.filter_by(hometown='New York').filter_by(university='USC').all()
Run Code Online (Sandbox Code Playgroud)
现在,我想根据变量的值动态生成这些查询.
例如,我的变量可能如下所示:
{'hometown': 'New York'}
Run Code Online (Sandbox Code Playgroud)
或者像这样:
{'university': 'USC'}
Run Code Online (Sandbox Code Playgroud)
......或者甚至是这样的:
[{'hometown': 'New York'}, {'university': 'USC'}]
Run Code Online (Sandbox Code Playgroud)
你可以帮我编写一个以字典(或字典列表)作为输入的函数,然后动态构建正确的sqlalchemy查询吗?
如果我尝试为关键字使用变量,我得到这个错误:
key = 'university'
User.query.filter_by(key='USC').all()
InvalidRequestError: Entity '<class 'User'>' has no property 'key'
Run Code Online (Sandbox Code Playgroud)
其次,我不确定如何动态地将多个filter_by表达式链接在一起.
我可以明确地调出一个filter_by表达式,但是如何根据变量将几个链接在一起呢?
希望这更有意义.
谢谢!
javascript ×4
python ×4
flask ×3
reactjs ×3
sqlalchemy ×3
jestjs ×2
atom-editor ×1
encode ×1
enzyme ×1
escaping ×1
freemarker ×1
java ×1
sql ×1
unicode ×1
utf-8 ×1