After fixing some corner cases of #7560[1], and realizing that it is
too easy to write a wrong get_db_prep_lookup method (example:
#7448[2]), I have reached the conclusion that there is no reason to
have *two* methods for doing the python -> db conversion
(``get_db_prep_save`` and ``get_db_prep_lookup``). Not for most of the
fields, at least.

What I'm talking about is that, to ensure the proper type conversion
before querying the database[3], all of django core Fields should
currently do this:

def get_db_prep_save(self, value):
    return some_transformation(value)

def get_db_prep_lookup(self, value, lookup_type):
    if hasattr(value, 'as_sql'):
        sql, params = value.as_sql()
        return QueryWrapper(('(%s)' % sql), params)
    if lookup_type in ('range', 'in'):
        value = [some_transformation(v) for v in value]
    elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):
        value = some_transformation(v)
    return Field.get_db_prep_lookup(self, value)

Which blatantly violates DRY.

Also note that the documentation for custom fields, suggest to reuse
get_db_prep_save on get_db_prep_lookup[4]. Following that advice, I
propose to add a ``get_db_prep_value`` method to the Field API, to
make the common case much simpler. Then, almost all field can do:

def get_db_prep_value(self, value):
    return some_transformation(value)

[Or just implement the transformation in the body, if it is not
trivial. I didn't want to reuse get_db_prep_save as the generic
transformation method, because the 'save' part seems to imply more
than just value transformation].

Then, on the base Field we would use this method to implement
``get_db_prep_save`` and ``get_db_prep_lookup``, and stop repeating
ourselves.

Does that make sense?

Should I add this change to the patch on #7560?

[1] http://code.djangoproject.com/attachment/ticket/7560
[2] http://code.djangoproject.com/attachment/ticket/7448
[3] This is needed by some backends which are not "string-happy", such
as JDBC backends.
[4] 
http://www.djangoproject.com/documentation/custom_model_fields/#get-db-prep-lookup-self-lookup-type-value
-- 
Leo Soto M.
http://blog.leosoto.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to