Django makemigrations出了点问题,如下代码:

class User(AbstractBaseUser, PermissionsMixin):

    nickname = models.CharField(max_length=30, blank=True, verbose_name=u'')
    is_staff = models.BooleanField(default=False, verbose_name=u'')
    is_active = models.BooleanField(default=True, verbose_name=u'')

    objects = UserManager()


当我运行python manage.py makemigrations时,字段is_staff不会显示在迁移文件中。我不知道为什么这个领域消失了。但是,如果我将名称更改为is_aa,则显示正确。

迁移文件为:

class Migration(migrations.Migration):

initial = True

dependencies = [
    ('auth', '0008_alter_user_username_max_length'),
]

operations = [
    migrations.CreateModel(
        name='User',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('password', models.CharField(max_length=128, verbose_name='password')),
            ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
            ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
            ('nickname', models.CharField(blank=True, max_length=30, verbose_name='\u6635\u79f0')),
            ('is_active', models.BooleanField(default=True, verbose_name='\u662f\u5426\u6709\u6548')),
            ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
            ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
        ],
        options={
            'db_table': 'user',
        },
        managers=[
            ('objects', apps.api.user.models.UserManager()),
        ],
    ),

最佳答案

is_staff = models.BooleanField(_('staff status'), default=False)


并将其添加到您的模型中:

def get_group_permissions(self, obj=None):
    return self.is_staff


使用它,因为is_staffPermissionMixin已经存在,因此发生冲突。

10-06 15:22