我试图检查Charfield(charfield_1)的前3个字符是否与同一模型的另一个Charfield(charfield_2)相似.
尝试:
User.objects.filter(charfield_2__startswith=Substr('charfield_1', 1, 3))
Run Code Online (Sandbox Code Playgroud)
使用F和Func尝试没有任何成功.我一直在:
django.db.utils.DataError: invalid input syntax for integer: "1%"
LINE 1: ...CE(REPLACE((SUBSTRING("model_name"."charfield_2", '1%', 3)),...
Run Code Online (Sandbox Code Playgroud)
知道如何使这项工作?我想使用ORM的解决方案来避免性能问题.
更新:
检查ORM生成的查询和错误消息后,当我使用startswith或包含查找表达式时,看起来第二个Substr参数被非整数替换.
例如:Substr('charfield_1',1,3)被Substr替换('charfield_1','%1%',3)
我使用的是2.0.2版.
已打开并接受票证:https: //code.djangoproject.com/ticket/29155
除了在 ngOnDestroy 中取消订阅之外,是否有办法避免对组件中的 BehaviourSubject 进行重复订阅?到目前为止,这是我发现在在可观察对象上创建订阅的组件上来回导航时避免重复订阅的唯一方法。
例子:
一项用户服务
@Injectable()
export class UserService {
constructor(private http: Http) {
this.setCurrentUser();
}
private currentUser$ = new BehaviorSubject<User>(null);
public getCurrentUser(): Observable<User> {
return this.currentUser$.asObservable();
}
public setCurrentUser(): void {
this.getLoggedUser(); //
}
private getLoggedUser(): void {
let getCurrentUserUrl = 'http://127.0.0.1:8000/users/current/'
let headers = new Headers({
'Content-Type': 'application/json'
});
let options = new RequestOptions({
headers: headers
});
options.withCredentials = true;
this.http.get(getCurrentUserUrl, options)
.map(this.toUser)
.catch(this.handleError)
.subscribe(
user => this.currentUser$.next(user),
error => console.log("Error subscribing to currentUser: " + …Run Code Online (Sandbox Code Playgroud)