My idea is probably not unique and Django is already moving in this
direction. I apologize if this is the case.

I have thought for a year, pertaining to Django, about one thing: Why
doesn't validation propagate from the lowest level? Why should this
have to take place at a higher level? What happens with I want to do
things without using forms? Then, Django 1.2 came from the heavens
with validation in the models (which I currently use and love). The
only problem is that it still handles all validation (except the
basics "This field is required" validation) in the form level and a
special model field's only real functionality (outside of choosing a
DB column type and serializing if necessary) is to specify the form
field, which then does the validation necessary.

For instance, in the case of IPAddressField:

# Form
class IPAddressField(CharField):
    default_error_messages = {
        'invalid': _(u'Enter a valid IPv4 address.'),
    }
    default_validators = [validators.validate_ipv4_address]

# Model
class IPAddressField(Field):
    empty_strings_allowed = False
    description = _("IP address")
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 15
        Field.__init__(self, *args, **kwargs)

    def get_internal_type(self):
        return "IPAddressField"

    def formfield(self, **kwargs):
        defaults = {'form_class': forms.IPAddressField}
        defaults.update(kwargs)
        return super(IPAddressField, self).formfield(**defaults)

I would rather use:

# Form
# Nothing, the model field could just use a CharField() instance in
this case.

# Model
class IPAddressField(Field):
    default_error_messages = {
        'invalid': _(u'Enter a valid IPv4 address.'),
    }
    default_validators = [validators.validate_ipv4_address]
    empty_strings_allowed = False
    description = _("IP address")
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 15
        Field.__init__(self, *args, **kwargs)

    def get_internal_type(self):
        return "IPAddressField"

    def formfield(self, **kwargs):
        defaults = {'form_class': forms.CharField}
        defaults.update(kwargs)
        return super(IPAddressField, self).formfield(**defaults)

Is this the direct that Django's validation is moving to, or am I
thinking of this all wrong?

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

Reply via email to