On Saturday 09 February 2013, Yo-Yo Ma wrote:
> A form that has a char field (e.g. "name") when provided a dict of data
> will convert the value of "name" to a Unicode, no matter what. I understand
> that this is desirable in some circumstances, but it can lead to things
> 
> like:
> >>> product.name
> 
> u"{'haha': 'I am a dict'}"
> 
> Perhaps this is desirable, but I wonder if there is any merit to the idea
> of sanitizing data to ensure it is "valid" for a char field, since
> practically *any* Python object can be cast into a Unicode (vs a
> DecimalField or an IntegerField, which will raise an error).
> 
> I realize the distinction of a "valid" would be completely arbitrary (e.g.,
> who's to say that a dict isn't a valid char field value, but an that
> integer is?), but nonetheless, here I am, requesting feedback.

I think an important point to keep in mind is that it isn't just 'dict', but 
also ArbitraryUserClass which is considered valid today, and losing this will 
break many users' code. So I'm -1 on adding such validation to the core 
CharField.

There may be merit to such validation in special cases -- for this, it is easy 
enough to subclass CharField and override its to_python() method (that's the 
one that converts the value to unicode). Something along the lines of

class StrictCharField(CharField):
        def to_python(self, value):
                if isinstance(value, basestring):
                        return super(StrictCharField, self).to_python(value)
                else:
                        raise TypeError('Get off my lawn')

(this is untested python-2-only pseudocode, YMMV).

My 2 cents,
        Shai.

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


Reply via email to