I'm new to Django. I'm working on a website that will only contain users as staff users. I'm the administrator. My plan is to register a user and give him his username and password to login; I want him only to be able to edit his profile to add more information, not to change existing attributes. How can I do that?
The easiest way would probably be to create a appropriate ModelForm and a corresponding view, that checks that the instance the user want's to update is the own instance.
I you want to integrate this directly in the admin backend, you can also do this. The most djangoish way would propably be to create a own ModelAdmin class for the User that has the right methods overridden (see the methods startinghere). I think you should start with overriding has_change_permission where you can check if the object the user tries to edit, is his own and return False otherwise.
To replace the standard User ModelAdmin you need to do a little fiddeling in the admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
# add the code here
# deregister the standard AdminModel and register the own one
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
(I'm using this code on a live site and it works create)
Related
I want to have a custom tab for staff users so in admin.py I do:
class StaffUser(User):
class Meta:
proxy = True
#admin.register(StaffUser)
class AdminUserAdmin(admin.ModelAdmin):
pass
But on the admin site, whenever I add a new user with this interface I can't log in with it since for some reason it sets it's password as plain text instead of hashing it.
I've read this post BUT if I do that and change StaffUser to inherint from AdminUserI get this other error
AttributeError: type object 'StaffUser' has no attribute '_meta'
You should declare your models in models.py (or a models package), not admin.py.
UserAdmin (there is no such thing as an AdminUser in the Django packages) is a subclass of ModelAdmin, that is, a class for registering models in the Django admin. It is not a model subclass that you can extend to make new models.
Also, if you are trying to extend the User just to distinguish users with access to the Django admin site, note that there is already an is_staff property in the default User model. And in any case, if you still want to extend the User model, you should be extending AbstractUser instead as stated in the docs.
I'm reading through these docs on Django:
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending-user
I'm just not sure which route to go. They suggest using a custom user model like so:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
Doing so, what would the procedure be to make two different user types. Should I have something like this:
class Client(AbstractUser):
pass
class Employee(AbstractUser):
pass
But then how would new registered users be 'clients' when they sign up? And how would I make it so they see a different part of the site than employees?
I'm just looking for some guidance in how I should approach this.
Are you only addressing a question of permissions, or do 'Client' and 'Employee' need their own specific functionality?
If it is just about permissions, then it would be easier to use the permissions functionality of the auth library.
You can then protect your views with the permissions decorator.
From the docs:
from django.contrib.auth.decorators import permission_required
#permission_required('polls.can_vote', login_url='/loginpage/')
def my_view(request):
...
So my question is what should I look for creating a page which will allow user to add some information after the registration. I took a look at Django Profiles, but it requires lower version of Python (2.7), if I'm not mistaken.
Another thing is I need to create two types of users - I'm thinking of maybe #permission to implement it, but another point is that I want to include something like checkbox while registration, and if user chooses one type of user, he will be allowed to see default account page for this type of user which he should fill up.
I'm running Django 1.10.5 and Python 3.6.0.
Thanks in advance.
If you want to add custom fields to your user object take a look at custom user model django implementation. Then, for updating user object you can just use generic update view, it will look something like this:
from django.contrib.auth import get_user_model
class UserUpdateView(UpdateView):
model = get_user_model()
fields = ['field1', 'field2', 'field3']
template_name = "core/user_edit.html"
I'm using a custom User admin by:
class CustomUserAdmin(admin.ModelAdmin):
model = User
...
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
but when I try to change the password via the admin page I get a 404.
user object with primary key u'4/password' does not exist.
Reverting back to the default User admin works fine.
The default UserAdmin in django.contrib.auth.admin implements a lot of functionality, including the change password page.
Your CustomUserAdmin should subclass UserAdmin instead of admin.ModelAdmin, unless you want to reimplement that functionality yourself.
class CustomUserAdmin(UserAdmin):
# as an example, this custom user admin orders users by email address
ordering = ('email',)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
Also:
As per the docs, if you inherit from AbstractBaseUser you cannot use the default UserAdmin; or, put another way, you can but only a subset of the functionality will work - changing an existing password might work, but adding a new user will throw exceptions.
Hi ive not be able to find any one trying to solve this issue yet so i thought id ask the question here. Basically im trying to add an image field to the admin.auth users admin site that relates to my userprofile model.
Aswell as users that register to the site, the admins need to be able to specify a profile picture when the create a user in the admin site that will be used for posts they make to news and blogs etc.
Is this at all possible, i have the userprofile model working which will allow me to add the image but i want to include this in the admin user panel when an admin creates a new user.
Any help is greatly appreciated!!
Thanks in advance
You can add your UserProfile (with the extra image field) as an inline to the User adminModel. So when you go to the admin.auth panel to add a new user, there will be an inline below it with all the UserProfile fields.
Something like this should do it (in admin.py of whatever app your userprofile is in):
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib import admin
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserModelAdmin(UserAdmin):
inlines = [UserProfileInline,]
admin.site.unregister(User)
admin.site.register(User,UserModelAdmin)