我正在用SQLAlchemy后端编写一个应用程序,该应用程序具有一个模型,该模型的字段定义如下:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
class MyObject(db.Model):
user = relationship(User, primaryjoin=user_id==User.id)
用户通过Flask-Login登录。
在视图中,我会遇到某种奇怪的行为:
36 import ipdb; ipdb.set_trace()
---> 37 if theobject.user != current_user:
38 abort(403)
这总是失败,并显示403。
ipdb> theobject.user
<User emes [emes@test.com]>
ipdb> current_user
<User emes [emes@test.com]>
ipdb> theobject.user == current_user
False
为什么会发生?实现User.__eq__
是到达此处的正确方法吗?
答案 0 :(得分:1)
您可以根据ID进行比较吗? 例如
@ManagedBean(name = "LoginBean")
@SessionScoped
public class LoginBean
{
String username;
String password;
public String login() throws Exception
{
Scanner s = new Scanner(new URL("http://172.17.0.3:8080/login?username=" + username + "&password=" + password).openStream(), "UTF-8").useDelimiter("\\A");
String out = s.next();
s.close();
Boolean bool = new Gson().fromJson(out, Boolean.class);
if (bool)
{
return "userPage";
} else
{
return "index";
}
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}