开发者

Can I override a query in DJango?

I know you can override delete and save methods in DJango models, but can you override a select query somehow to intercept and change a parameter 开发者_Python百科slightly. I have a hashed value I want to check for, and would like to keep the hashing internal to the model.


You don't make it absolutely clear what you want to do, but I think there are two possibilities here.

The general way to override the database query is to define a custom Manager, and override get_query_set method. You can add extra filtering criteria here.

However, if I understand your question properly, you are trying to change the query for a particular field only. In this case, I think the better answer is to define a custom Field. Here you can override get_db_prep_lookup which allows you to customise the value that is used in the database lookup.


if you're using 1.2, you can try raw(), which seems like exactly what you're looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜