tags:

views:

53

answers:

2

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 slightly. I have a hashed value I want to check for, and would like to keep the hashing internal to the model.

+3  A: 

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.

Daniel Roseman
Perfect, you nailed it. Essentially I have passwords I have to hash and store, and I wanted the model to handle all hashing during saves and updates, and to hash the password it is passed when validating the user. I think DJango models and admin could use a hashed field type.
stinkypyper
+1  A: 

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

nbv4