我在 app2 中有2个模型类 A类(models.Model), app2 <中有类B(A) / strong>如下:
#app1/models.py
class CustomImageField(models.ImageField):
"""
Same as ImageField, but you can specify:
* max_upload_size - a number indicating the maximum file size allowed for upload.
5MB - 5242880
"""
def __init__(self, *args, **kwargs):
self.max_upload_size = kwargs.pop("max_upload_size")
super(CustomImageField, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
print "inside CustomImageField - clean()"
data = super(CustomImageField, self).clean(*args, **kwargs)
file = data.file
try:
if file._size > self.max_upload_size:
raise ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size)))
except AttributeError:
pass
return data
class A(models.Model):
logo = CustomImageField(upload_to = upload_path, blank = True, help_text = "Image Files only. Max Size 5MB", max_upload_size = 5242880, verbose_name='Company Logo')
...
# app2/models.py
from app1.models import A
class B(A):
...
CustomImageField的上传文件限制检查不起作用,因为我没有调用clean(),因为我可以从trace中看到。当我保存B类对象时,你能给我一个在CustomImageField中调用clean方法的方法。