Django AbstractUser not working properly - django

I am trying to inherit from AbstractUSer my models.py looks like:
class MyUser(AbstractUser):
created = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username',]
MyUser._meta.get_field_by_name('email')[0]._unique=True
now by declaring email as unique field and username as a required field my superuser is being created successfully and also is being authenticated properly but I am having a problem while creating any other user as if I am creating any user through my admin page its not being authenticated.It always returns
None
My admin.py:
from django.contrib import admin
from credilet.models import *
admin.site.register(MyUser)
What I am thinking is that the create_user is not being called properly as if I see in my admin page the password is not hashed so that means the create_user is not being called properly.Somebody please help through it or even if you have a proper documentation on abstractuser
not abstractbaseuser
so please refer that to me in the solutions.
Thanks

If you want to change the authentication system you have to use AbstractBaseUser,
look at this full example.
AbstractUser is ok to Extend Django’s default User.

I think you should add UserAdmin into admin.py for Myuser
if you does not add UserAdmin , password can't be hashed with django:
from django.contrib import admin
from credilet.models import *
from django.contrib.auth.admin import UserAdmin
admin.site.register(MyUser, UserAdmin) # add UserAdmin

Related

List of contacts per User when extending AbstractUser

I want to create a custom user model with some extra fields, among which a contact list of other users. I would like to extend AbstractUser instead of creating a new model with a one-to-one link to User.
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
a_custom_field = models.IntegerField(default=0)
# per-user contact list
contacts = models.ManyToManyField(get_user_model())
This code doesn't work. Throwing this error during makemigrations.
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.CustomUser' that has not been installed
The error totally makes sense, but what's the right way to achieve this?
I found the solution just by digging more into the django docs.
The problem is that I cannot use get_user_model() before the user model has been created.
The solution is using the class name as a string. So, this code works great:
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
a_custom_field = models.IntegerField(default=0)
# per-user contact list
contacts = models.ManyToManyField('CustomUser')

Unable to register the default user model with django.contrib.admin

I'm trying to manage my User table with django.contrib.admin
But during I add the User table in admin, I had an issue that doesn't appear in admin site.
Here is my code.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
#admin.register(User)
class CustomUserAdmin(UserAdmin):
pass
assert admin.site.is_registered(User) # Fails here
And when I added the other custom model, it works.
Thanks
When you modify or implement the user model, you have to be aware of AUTH_USER_MODEL.
I changed AUTH_USER_MODEL in setting.py, and it starts to work.
Thanks.

Django Admin not hashing user's password

I am using AbstractBaseUser and UserCreationForm with my Django app. When registering users through my app, the password gets saved in hash format and saved in the database. But when I try to do the same thing using Django admin site, the password gets saved in raw format.
You need to make sure that your model admin class knows how to hash passwords. According to the docs, if you are using subclassing AbstractBaseUser, then you might be able to extend UserAdmin.
Assuming your custom user model is called CustomUser, you could try the following.
from django.contrib.auth.admin import UserAdmin
class CustomUserAdmin(UserAdmin):
...
admin.site.register(CustomUser, CustomUserAdmin)
I guess the problem is that you inherited ModelAdmin instead of UserAdmin from django.contrib.auth.admin in your admin.py.
Sample code:
from django.contrib.auth.admin import UserAdmin
from .models import Employee
class EmployeeAdmin(UserAdmin):
pass
admin.site.register(Employee, EmployeeAdmin)

How can I add additional required fields to Django's "User Add" administration page?

When adding a new user to my Django application, I'd like to ensure that the administration page requires an administrator to include the "email" field.
I've tried tinkering with the UserAdminForm object, but I've come up empty thus far. Any tips?
You'll want to add something like this to one of your project's admin.py files:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import Group, User
# Override username field require email address
class UserCreationForm2(UserCreationForm):
email = forms.CharField(max_length=75, required=True)
class UserChangeForm2(UserChangeForm):
email = forms.CharField(max_length=75, required=True)
class UserAdmin2(UserAdmin):
form = UserChangeForm2
add_form = UserCreationForm2
admin.site.unregister(User)
admin.site.register(User, UserAdmin2)
Essentially, make the email field required, unregister the built-in admin, and register a new admin with the override.

Django admin - stackedInline single instance

I'm building a site based on a highly customized django admin instance and am running into issues with user profiles as an inline to user_admin
long story short regardless of what I set for max_num and extra in the admin.StackedInline instance it allows up to 2 profiles per user - with a blank one in place by default if the user has an existing profile
anyone know how I could adjust this to show only a single inline profile without resorting to some JS front end hack?
relevant code from: profiles.admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from profile.models import user_profile
class user_profile_admin(admin.StackedInline):
model = user_profile
fk_name = 'user'
max_num = 1
extra = 0
class user_admin_extended(UserAdmin):
inlines = [user_profile_admin, ]
admin.site.unregister(User)
admin.site.register(User, user_admin_extended)
I assume you're using FK field to connect user and profile? Try OneToOneField it should render just one inline in admin.