python - Override Django ImageField validation -
i trying create form users allowed upload image file + swf files. django's imagefield not support swf need override it.
what want check if file swf, if true, return it. if it's not swf, call original method take care of file validation.
however, not sure how implement that. here example of trying achieve, not work:
from hexagonit import swfheader class swfimagefield(forms.imagefield): def to_python(self, data): try: swfheader.parse(data) return data except: return super(swfimagefield, self).to_python(data)
what allowing only swf files @ moment.
an alternative , possibly easiest solution use standard filefield
custom validator:
def my_validate(value): ext = os.path.splitext(value.name)[1] # [0] returns path filename valid = ['.jpg', '.swf'] if ext not in valid: raise validationerror("unsupported file extension.") class myform(forms.form): file = forms.filefield(validators=[my_validate])
Comments
Post a Comment