tags:

views:

81

answers:

1

I'm more comfortable with PHP & MySQL, don't know a lick of Python (yet) except for the teeny tiny bit I picked up using the Django admin recently...therefore please excuse this very stupid question that I'm embarrassed to ask. In PHP this would be trivial for me...

I'm using a color picker (farbtastic) w/ Django Admin, it's the only one I like for this purpose. Except...it adds a # symbol prior to the hex value.

When the user hits submit, I'd like to validate the field before sending to the database and just strip off that # symbol.

Thanks in advance for helping this Django newbie :)

UPDATE:

Based on S. Lott's code here's what worked:

def save(self):
    if self.hexvalue.startswith("#"):
        self.hexvalue= self.hexvalue[1:]
    super(Color, self).save()
+1  A: 

Usually, you do a customized form to clean the data properly. It seems (at first) like overkill, but it's a very general solution.

You have to (1) define the modified Form, and then (2) bind the modified Form into the Admin interface.

On the other hand, you might be able to do this in the model's save method, which is simpler.

class MyThing( models.Model )
    color = models.CharField(...)
    def save( self, *args, **kw ):
        if self.color.startswith("#"):
            self.color= self.color[1:]
        super( MyThing, self ).save( *args, **kw )
S.Lott
Thanks Mate! Appreciate your help!
joedevon