Django 2.2 AbstractUser和迁移问题

时间:2019-04-30 15:04:19

标签: django django-models

我这样扩展Django用户模型:

#core.models

class Institute(models.Model):
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message=institute_phone_help_text)

    name = models.CharField(_('name'), max_length=255)
    description = models.TextField(_('description'), blank=True)
    abbreviation = models.CharField(_('abbreviation'), blank=True, max_length=100)
    address = models.TextField(_('address'), blank=True)
    phone = models.CharField(validators=[phone_regex], max_length=17, blank=True)  # validators should be a list
    websites = ArrayField(models.URLField(max_length=255), verbose_name=_('websites'), blank=True, null=True)

    class Meta:
        verbose_name = _('institute')
        verbose_name_plural = _('institutes')

    def __str__(self):
        return '{0} ({1})'.format(self.name, self.abbreviation)


class User(AbstractUser):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    institute = models.ForeignKey(Institute, on_delete=models.CASCADE)
    params = JSONField(_('params'), null=True, blank=True,)
    about_me = models.TextField(_('about me'), blank=True,)

每次启动./manage.py makemigrations core时,如果数据库为空,它将始终创建一个新的迁移文件

import django.contrib.auth.models
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0002_auto_20190430_1655'),
    ]

    operations = [
        migrations.AlterModelManagers(
            name='user',
            managers=[
                ('objects', django.contrib.auth.models.UserManager()),
            ],
        ),
    ]

我尝试了不同的组合:

./manage.py makemigrations core
./manage.py migrate core
./manage.py migrate 

./manage.py makemigrations core
./manage.py makemigrations core

./manage.py makemigrations
./manage.py migrate

它总是创建迁移文件。

谢谢。

D

1 个答案:

答案 0 :(得分:0)

如果您使用抽象用户模型,请确保在迁移之前运行makemigrations。

我与Abstract用户有相同的问题, 我通过删除sqlite3文件以及所有迁移文件来解决它。 之后,我运行这两个命令:

python manage.py makemigrations python manage.py migration。

让我知道是否有帮助!