Python Django在迁移时无法识别自定义字段

Python Django在迁移时无法识别自定义字段,python,django,migration,Python,Django,Migration,我正在使用我在网上找到的以下自定义字段,但我忘记了在哪里: 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

我正在使用我在网上找到的以下自定义字段,但我忘记了在哪里:

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
它包含在my 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'

请显示完整的回溯。你一定把它称为模型。ContentTypeRestrictedFileField的某个位置,否则异常将是NameError:name。。。未定义。我提供了上面的完整回溯。我还检查了该文件,该字段只有一个实例,正如上面所示。在0001_initial.py中,我认为从gk_models.models导入ContentTypeRestrictedFileField是正确的,正如它所说的。在我的gk_models init.py中,我也导入了模型文件中的所有内容。