django admin-您无权编辑任何内容 [英] django admin - You don't have permission to edit anything

查看:46
本文介绍了django admin-您无权编辑任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了 django文档创建自定义用户模型,同时使用我自己的字段扩展模型本身.这样就变成了这样:

  class MyUser(AbstractBaseUser,PermissionsMixin):email = models.EmailField(max_length = 255,unique = True)first_name = models.CharField(最大长度= 35)last_name = models.CharField(最大长度= 35)用户名= models.CharField(max_length = 70,unique = True)date_of_birth = models.DateField()is_active = models.BooleanField(默认= True)is_admin = models.BooleanField(默认= False)@财产def is_staff(self):返回self.is_admindef get_full_name(self):return('%s%s')%(self.first_name,self.last_name)def get_short_name(self):返回self.username对象= MyUserManager()USERNAME_FIELD ='电子邮件'REQUIRED_FIELDS = ['first_name','last_name','username','date_of_birth'] 

其管理者应为:

  class MyUserManager(BaseUserManager):def create_user(自己,电子邮件,名字,姓氏,用户名,出生日期,密码=无):如果不是电子邮件:引发ValueError('用户必须具有电子邮件地址')用户= self.model(email = self.normalize_email(电子邮件),first_name =名字,last_name =姓氏,用户名=用户名,date_of_birth =出生日期date_of_birth)user.set_password(密码)user.save(using = self._db)回头用户def create_superuser(自己,电子邮件,名字,姓氏,用户名,出生日期,密码):用户= self.create_user(电子邮件,first_name =名字,last_name =姓氏,用户名=用户名,date_of_birth =出生日期date_of_birth密码=密码)user.is_admin =真user.save(using = self._db)回头用户 

但是,当我在syncdb中创建超级用户后,登录到管理面板后,无需执行任何操作.它显示:

您无权编辑任何内容.

我看到其他一些帖子也遇到了同样的问题,其中大多数建议在urls.py中添加 admin.autodiscover().但这甚至没有帮助我.

这是admin.py:

  class MyUserAdmin(UserAdmin):形式= UserChangeFormadd_form = UserCreationFormlist_display =(电子邮件",名字",姓氏",用户名",出生日期","is_admin")list_filter =('is_admin',)字段集=((无,{'fields':('email','password')}),(个人信息",{字段":((("first_name","last_name"),"username","date_of_birth")))),('Permissions',{'fields':('is_admin',)}),)add_fieldsets =((没有任何, {'classes':('Wide',),字段" :(电子邮件",名字",姓氏",用户名",出生日期")}),)search_fields =('电子邮件',)订购=('电子邮件',)filter_horizo​​ntal =()admin.site.register(MyUser,MyUserAdmin) 

我在这里做错了什么?请帮助我如何解决这个问题.谢谢.

解决方案

在这里回答我的问题.所以问题是,PermissionsMixin.我以为PermissionsMixin将处理所有与迁移相关的事情.但是,即使我将PermissionsMixin添加到新的用户模型中,我也必须通过添加 is_superuser has_perm 明确地告诉django超级用户确实是超级用户.

因此我将用户管理器中的功能更改为

  def create_superuser(自己,电子邮件,名字,姓氏,用户名,生日,密码,**):用户= self.create_user(电子邮件,first_name =名字,last_name =姓氏,用户名=用户名,date_of_birth =出生日期date_of_birth密码=密码,is_superuser = True,**夸克) 

希望这会对像我这样的人有所帮助!

I followed the django doc on creating a custom user model while extending the model itself with my own fields. So it became like this:

class MyUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    first_name = models.CharField(max_length=35)
    last_name = models.CharField(max_length=35)
    username = models.CharField(max_length=70, unique=True)
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    @property
    def is_staff(self):
        return self.is_admin

    def get_full_name(self):
        return ('%s %s') % (self.first_name, self.last_name)

    def get_short_name(self):
        return self.username

    objects = MyUserManager()
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'username', 'date_of_birth']

And its manager to be:

class MyUserManager(BaseUserManager):
    def create_user(self, email, first_name, last_name, username, date_of_birth, password=None):
        if not email:
            raise ValueError('User must have an email address')

        user = self.model(
            email=self.normalize_email(email),
            first_name=first_name,
            last_name=last_name,
            username=username,
            date_of_birth=date_of_birth,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, first_name, last_name, username, date_of_birth, password):
        user = self.create_user(
            email,
            first_name=first_name,
            last_name=last_name,
            username=username,
            date_of_birth=date_of_birth,
            password=password
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

However, after I created the superuser while syncdb, when I login to the admin panel, there is nothing to do. It displays:

You don't have permission to edit anything.

I saw some other post with the same problem and most of them suggested to add admin.autodiscover() in the urls.py. But even this didn't help me.

This is the admin.py:

class MyUserAdmin(UserAdmin):
    form = UserChangeForm
    add_form = UserCreationForm

    list_display = ('email', 'first_name', 'last_name', 'username', 'date_of_birth', 'is_admin')
    list_filter = ('is_admin',)
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': (('first_name', 'last_name'), 'username', 'date_of_birth')}),
        ('Permissions', {'fields': ('is_admin',)}),
    )

    add_fieldsets = (
        (None, {
            'classes': ('Wide',),
            'fields': ('email', 'first_name', 'last_name', 'username', 'date_of_birth')
        }),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()


admin.site.register(MyUser, MyUserAdmin)

What am I doing wrong here? Please help me how to solve this problem. Thank you.

解决方案

Answering my question here. So the problem was, PermissionsMixin. What I thought was that PermissionsMixin will handle all the permssion related things. But even though I added the PermissionsMixin to the new user Model, I had to explicitly make the tell django that the superuser is indeed superuser by adding is_superuser or by has_perm.

So I changed my function inside the User manager to

def create_superuser(self, email, first_name, last_name, username, date_of_birth, password, **kwargs):
        user = self.create_user(
            email,
            first_name=first_name,
            last_name=last_name,
            username=username,
            date_of_birth=date_of_birth,
            password=password,
            is_superuser=True,
            **kwargs
        )

Hope this will help someone like me!

这篇关于django admin-您无权编辑任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆