问题
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.
回答1:
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!
回答2:
in 2020, you need to define two methods
def has_module_perms(self, app_label):
return self.is_superuser
def has_perm(self, perm, obj=None):
return self.is_superuser
also the model field is_superuser...you can rename the is_superuser field to is_admin or anything... for that case u will also need to change is_superuser to is_admin in your functions
来源:https://stackoverflow.com/questions/33556515/django-admin-you-dont-have-permission-to-edit-anything