2017-06-22 231 views
1

一些與Django的makemigrations錯去,像下面的代碼: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()), 
     ], 
    ), 
+0

只是刪除'property' is_staff – zimmer

+0

是否[這](https://docs.djangoproject.com/en/1.11/topics/auth/customizing /#更改爲自定義用戶模型 - 項目中)適用於您的情況? – Melvyn

+0

是的,財產和領域是重複的,只是刪除財產 – zimmer

回答

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

,並添加到您的模型:

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

利用這一點,因爲is_staff已經與PermissionMixin存在,因此發生衝突的。

+0

我沒有看到'在'PermissionMixin''is_staff',它也不工作。 – zimmer

+0

刪除你的遷移文件一次,然後再試 – Exprator

+0

我嘗試了幾次,並且... – zimmer

0

嘗試將此添加到模型:

class Meta: 
    db_table = 'User' 
    managed = True 

隨着「管理=真正的」你是說要Django的,他需要管理所有遷移。

此更新應用後(如果是外部應用程序) 和運行

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