Django

Code

Show
Ignore:
Timestamp:
08/05/08 12:15:33 (5 months ago)
Author:
jbronn
Message:

gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis

    • Property svnmerge-integrated changed from /django/trunk:1-7978 to /django/trunk:1-8214
  • django/branches/gis/docs/custom_model_fields.txt

    r7979 r8215  
    112112of the class behavior. 
    113113 
    114 .. _form fields: ../newforms/#fields 
     114.. _form fields: ../forms/#fields 
    115115 
    116116It's important to realize that a Django field class is not what is stored in 
     
    386386mentioned earlier. Otherwise ``to_python()`` won't be called automatically. 
    387387 
    388 ``get_db_prep_save(self, value)`` 
    389 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     388``get_db_prep_value(self, value)`` 
     389~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    390390 
    391391This is the reverse of ``to_python()`` when working with the database backends 
     
    400400        # ... 
    401401 
    402         def get_db_prep_save(self, value): 
     402        def get_db_prep_value(self, value): 
    403403            return ''.join([''.join(l) for l in (value.north, 
    404404                    value.east, value.south, value.west)]) 
     405 
     406``get_db_prep_save(self, value)`` 
     407~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     408 
     409Same as the above, but called when the Field value must be *saved* to the 
     410database. As the default implementation just calls ``get_db_prep_value``, you 
     411shouldn't need to implement this method unless your custom field need a special 
     412conversion when being saved that is not the same as the used for normal query 
     413parameters (which is implemented by ``get_db_prep_value``). 
     414 
    405415 
    406416``pre_save(self, model_instance, add)`` 
     
    441451 
    442452If you needed to implement ``get_db_prep_save()``, you will usually need to 
    443 implement ``get_db_prep_lookup()``. The usual reason is because of the 
    444 ``range``  and ``in`` lookups. In these case, you will passed a list of 
    445 objects (presumably of the right type) and will need to convert them to a list 
    446 of things of the right type for passing to the database. Sometimes you can 
    447 reuse ``get_db_prep_save()``, or at least factor out some common pieces from 
    448 both methods into a help function. 
    449  
    450 For example:: 
     453implement ``get_db_prep_lookup()``. If you don't, ``get_db_prep_value`` will be 
     454called by the default implementation, to manage ``exact``, ``gt``, ``gte``, 
     455``lt``, ``lte``, ``in`` and ``range`` lookups. 
     456 
     457You may also want to implement this method to limit the lookup types that could 
     458be used with your custom field type. 
     459 
     460Note that, for ``range`` and ``in`` lookups, ``get_db_prep_lookup`` will receive 
     461a list of objects (presumably of the right type) and will need to convert them 
     462to a list of things of the right type for passing to the database. Most of the 
     463time, you can reuse ``get_db_prep_value()``, or at least factor out some common 
     464pieces. 
     465 
     466For example, the following code implements ``get_db_prep_lookup`` to limit the 
     467accepted lookup types to ``exact`` and ``in``:: 
    451468 
    452469    class HandField(models.Field): 
     
    456473            # We only handle 'exact' and 'in'. All others are errors. 
    457474            if lookup_type == 'exact': 
    458                 return self.get_db_prep_save(value) 
     475                return self.get_db_prep_value(value) 
    459476            elif lookup_type == 'in': 
    460                 return [self.get_db_prep_save(v) for v in value] 
     477                return [self.get_db_prep_value(v) for v in value] 
    461478            else: 
    462479                raise TypeError('Lookup type %r not supported.' % lookup_type) 
     
    494511fields. 
    495512 
    496 .. _helper functions: ../newforms/#generating-forms-for-models 
    497 .. _forms documentation: ../newforms/ 
     513.. _helper functions: ../forms/#generating-forms-for-models 
     514.. _forms documentation: ../forms/ 
    498515 
    499516``get_internal_type(self)`` 
     
    539556    serialization, the API might change in the future. 
    540557 
    541 Returns a dictionary, mapping the field's attribute name to a flattened string 
    542 version of the data. This method has some internal uses that aren't of 
    543 interest to use here (mostly having to do with manipulators). For our 
    544 purposes, it's sufficient to return a one item dictionary that maps the 
    545 attribute name to a string. 
     558Returns a dictionary, mapping the field's attribute name to a 
     559flattened string version of the data. This method has some internal 
     560uses that aren't of interest to use here (mostly having to do with 
     561forms). For our purposes, it's sufficient to return a one item 
     562dictionary that maps the attribute name to a string. 
    546563 
    547564This method is used by the serializers to convert the field into a string for 
     
    558575        def flatten_data(self, follow, obj=None): 
    559576            value = self._get_val_from_obj(obj) 
    560             return {self.attname: self.get_db_prep_save(value)} 
     577            return {self.attname: self.get_db_prep_value(value)} 
    561578 
    562579Some general advice