新模型迁移时出现ModuleNotFound错误

问题描述 投票:0回答:1

这是我的模型:

class Company(models.Model):
    class LTRCharField(models.CharField):
        def formfield(self, **kwargs):
            defaults = {'widget': TextInput(attrs={'dir': 'ltr'})}
            defaults.update(kwargs)
            return super().formfield(**defaults)

    title = models.CharField(_('company title'), max_length=255)
    logo = FileBrowseField(
        _('company logo'), max_length=255, directory='company/', extensions=['.png']
    )
    address = models.TextField(_('company address'))
    phone = LTRCharField(_('company phone'), max_length=20)

    class Meta:
        verbose_name = _('company')
        verbose_name_plural = _('company')

    def __str__(self):
        return self.title

migrate
管理命令给我这个错误:

ModuleNotFoundError: No module named 'company.models.Company'; 'company.models' is not a package

当我查看迁移文件时,我发现这一行有错误:

import company.models.Company

我不明白这里出了什么问题?

django
1个回答
0
投票

我相信你一定忘记通过在迁移文件夹中包含 __init__.py 文件来提醒 Python

migration(文件夹)
是一个包,而不仅仅是一个文件夹。如果没有上面的 init 文件,它就无法工作。这种情况通常发生在我们删除迁移文件夹并重新创建它而没有提醒 Python 它是我们想要执行的任何导入的包时。


#create this file inside your migration folder, and even check your company's app whether it has it, if not kindly create the file there also.
__init__.py
© www.soinside.com 2019 - 2024. All rights reserved.