我正在使用以下自定义字段(我在网上找到,虽然我忘了在哪里):
class ContentTypeRestrictedFileField(models.FileField):
def __init__(self, content_types=None, max_upload_size=None, *args, **kwargs):
self.content_types = content_types
self.max_upload_size = max_upload_size
super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)
file = data.file
try:
content_type = file.content_type
if content_type in self.content_types:
if file._size > self.max_upload_size:
raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size)))
else:
raise forms.ValidationError(_('File type not supported. Must be a pdf or jpeg.'))
except AttributeError:
pass
return data
def deconstruct(self):
name, path, args, kwargs = super(ContentTypeRestrictedFileField, self).deconstruct()
if (self.content_types is not None):
kwargs['content_types'] = self.content_types
if (self.max_upload_size is not None):
kwargs['max_upload_size'] = self.max_upload_size
return name, path, args, kwargs
它包含在我的models.py文件中,其中包含我的所有模型。他们都是。我正在使用以下模型中的字段,该字段再次在与自定义字段相同的文件中定义:
class AnswerModel(models.Model):
id = models.AutoField(primary_key=True)
application_id = models.ForeignKey(ApplicationModel, related_name="answers")
question_id = models.ForeignKey(QuestionModel, related_name="answers")
answer = models.SmallIntegerField(max_length=1, default=NOT_ANSWERED)
upload = ContentTypeRestrictedFileField(
upload_to=get_file_upload_path,
content_types=['application/pdf', 'image/jpeg', 'image/png'],
max_upload_size=2621440, # 2.5MB
null=True,
blank=True,
)
def __unicode__(self):
return self.answer
def __str__(self):
return self.answer
虽然当我尝试迁移时,我收到以下错误:
AttributeError: 'module' object has no attribute 'ContentTypeRestrictedFileField'
这对我来说没有任何意义,因为它是在使用之前定义的,在同一个文件中。
我在其他地方找不到任何答案;有没有人有任何想法?
干杯。
这是完整的追溯:
Development\Documents\GitHub\GKApplication\gk>manage.py migrate gk_models > traceback.txt
(most recent call last):
:\Users\Development\Documents\GitHub\GKApplication\gk\manage.py", line 10, in <module>
te_from_command_line(sys.argv)
:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
ty.execute()
:\Python27\lib\site-packages\django\core\management\__init__.py", line 377, in execute
fetch_command(subcommand).run_from_argv(self.argv)
:\Python27\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
execute(*args, **options.__dict__)
:\Python27\lib\site-packages\django\core\management\base.py", line 338, in execute
t = self.handle(*args, **options)
:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 63, in handle
tor = MigrationExecutor(connection, self.migration_progress_callback)
:\Python27\lib\site-packages\django\db\migrations\executor.py", line 17, in __init__
loader = MigrationLoader(self.connection)
:\Python27\lib\site-packages\django\db\migrations\loader.py", line 48, in __init__
build_graph()
:\Python27\lib\site-packages\django\db\migrations\loader.py", line 173, in build_graph
load_disk()
:\Python27\lib\site-packages\django\db\migrations\loader.py", line 103, in load_disk
tion_module = import_module("%s.%s" % (module_name, migration_name))
:\Python27\lib\importlib\__init__.py", line 37, in import_module
ort__(name)
:\Users\Development\Documents\GitHub\GKApplication\gk\gk_models\migrations\0001_initial.py", line 10, in <mod
Migration(migrations.Migration):
:\Users\Development\Documents\GitHub\GKApplication\gk\gk_models\migrations\0001_initial.py", line 79, in Migr
oad', gk_models.models.ContentTypeRestrictedFileField(content_types=[b'application/pdf', b'image/jpeg', b'image/png'], ma
size=2621440, null=True, upload_to=gk_models.models.get_file_upload_path, blank=True)),
Error: 'module' object has no attribute 'ContentTypeRestrictedFileField'