简体   繁体   中英

Django makemigrations doesn't work

Something wrong went with Django makemigrations, the code like below:

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()

When I run python manage.py makemigrations , the field is_staff doesn't show up in the migration file. I don't know why this field disappears. But if I change the name to is_aa , it shows right.

The migration file is:

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)

and add this to your model:

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

Use this, as is_staff already exists with PermissionMixin , thus the conflict happens.

Try to add this to the model:

class Meta:
    db_table = 'User'
    managed = True

With "managed = True" you are saying to django that he need to manage all migrations.

After this update your app (if is an external app) and run

python manage.py makemigrations user
python manage.py showmigrations user (to check if the migration is now correct)
python manage.py migrate user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM