I have a need for extending some of the fields provided by django to do
a whole host of things from the simple and obvious to the sick and
twisted. I have stuff that is working but I was wondering if what I was
doing was the right way to go about it instead of the various bits of
pounding I have been doing.

Here is the simplest example - I needed a NullBooleanField() but I
wanted the selection to
not be what was provided but instead be "Not set" "Yes" and "No."  I
could find no obvious way to just replace the Form used by the
NullBooleanField so I did the following (and yes it works currently)

import django.forms
import django.db.models.fields
import gecko.forms

# DATA_TYPES maps a django field type classes to the underlying
# database primitive. These can be different for each database backend
we
# are talking to. We try to only make new types based on existing
types.
#
# This is where we add our custom gecko defined datatypes and map them
# have the same underlying datatype as an existing django field type.
#
db = django.db.get_creation_module()
db.DATA_TYPES['GeckoNullBooleanField'] =
db.DATA_TYPES['NullBooleanField']

#############################################################################
#
class GeckoNullBooleanField(django.db.models.fields.NullBooleanField):
    """The GeckoNullBooleanField is just like the django
NullBooleanField
    except the form uses 'Not set' instead of 'Unknown' for Null/None.
    """
    def __init__(self, *args, **kwargs):
        django.db.models.fields.NullBooleanField.__init__(self,
*args,**kwargs)

    def get_manipulator_field_objs(self):
        return [gecko.forms.GeckoNullBooleanField]

In my forms.py file I have the form used by the above:

import django.forms
#############################################################################
#
class GeckoNullBooleanField(django.forms.NullBooleanField):
    """This is just like the formfields.NullBooleanField except we use
the
    string 'Not set' instead of 'Unknown'
    """
    def __init__(self, field_name, is_required=False,
validator_list=None):
        if validator_list is None:
            validator_list = []
        django.forms.SelectField.__init__(self, field_name,
                                          choices=[('1', 'Not set'),
                                                   ('2', 'Yes'),
                                                   ('3', 'No')],
                                          is_required=is_required,

validator_list=validator_list)

    def render(self, data):
        if data is None: data = '1'
        elif data == True: data = '2'
        elif data == False: data = '3'
        return django.forms.SelectField.render(self, data)

    def html2python(data):
        return {'1': None, '2': True, '3': False}[data]
    html2python = staticmethod(html2python)

A sicker example is where I am replacing a ManyToManyField selection
with a forms.TextField whose data is a comma separated list of one of
the fields for each of the data objects. I am having my custom Form
keep a reference to the list of data it is passed when it generates the
HTML representation of the data, and to re-hydrate this data in to a
list of objects in the html2python() method based on the list of
objects it got in the render() method.

This all seems to do nice things for me especially since it moves the
representation and validation of this data in to a module whose job is
to do that, just that the provided django fields and forms were not
what I was after.

Am I twisting things too far out of shape doing it this way? Is there
something more obvious and simpler that I am just missing? Most of
these are cases where all I really need to do is replace the 'form'
class instance used by the 'field' class instance.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to