Hi,
I am trying a custom field with a special data field have to handle.
The data is a 4 digit with or with a letter at the end. eg. 9(a),
99(a), 999(a), 9999(a).

class CustCharField(models.CharField):
    def __init__(self, *args, **kwargs):
        models.CharField.__init__(self, max_length=5, *args, **kwargs)

#    def to_python(self, value):
#        if (not value[:-1].isdigit()) or (len(value[:-1]) > 4):
#           raise validators.ValidationError, _(u'Seq is 4 numbers max
with at most 1 letter only trailing, eg. 9(a), 99(a), 999(a),
9999(a)')
#        if value[-1].isdigit():
#            return  "%04d" % int(value)
#        else:
#            return "%04d%s" % (int(value[:-1]), value[-1])

    def validate(self, field_data, all_data):
        if (not field_data[:-1].isdigit()) or (len(field_data[:-1]) >
4):
            raise validators.ValidationError, _(u'Seq is 4 numbers max
with at most 1 letter only trailing, eg. 9(a), 99(a), 999(a),
9999(a)')

    def get_internal_type(self):
        return "CharField"

class Test(models.Model):
    cust = CustCharField()

When I try to use shell, it seems it works:
>>> from mysite.testapp import models
>>> c1 = models.Test()
>>> b1.cust = "a"
>>> b1.validate()
{'cust': [u'Seq is 4 numbers max with at most 1 letter only trailing,
eg. 9(a), 99(a), 999(a), 9999(a)']}
>>>

But when I use the admin page, it doent show up an alert message
asking to change the field value, and then store it to database with
no exception. If I comment the to_python function about out. The admin
page does encouter an exception, but it halts instead of pop up alert
message.

Any clue on how this can be fix? TIA

Best,
ViX


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to