An improved custom user model for django projects
Project description
Django Modern User
Django Modern User is a custom user model for Django projects that replaces the default username
field with a case-insensitive email
field for authentication, and removes the requirement for first and last names. This model aims to provide a more practical and modern approach to user management in Django.
Installation
[!IMPORTANT] The instructions below are intended for integrating
django-modern-user
into new projects. Incorporating this package into existing projects, especially those with existing user data, can be complex and requires careful database migrations and potentially some code adjustments. The integration into projects with existing users is beyond the scope of this documentation.
-
Install
django-modern-user
via pip:python -m pip install django-modern-user
-
Add
django_modern_user
to yourINSTALLED_APPS
in your Django settings:INSTALLED_APPS = [ # ... other apps 'django_modern_user', ]
-
Create a new user model in your project by subclassing
django_modern_user.ModernUser
:# In your models.py from django_modern_user.models import ModernUser class CustomUser(ModernUser): pass
-
Update your Django settings to use your new user model:
AUTH_USER_MODEL = "<your_app_name>.CustomUser"
-
To use the provided
ModernUserAdmin
class in your Django admin site, you can subclass it in youradmin.py
file:from django.contrib import admin from django_modern_user.admin import ModernUserAdmin from .models import CustomUser @admin.register(CustomUser) class CustomUserAdmin(ModernUserAdmin): pass
-
Run migrations to create the necessary database table:
python manage.py migrate
This setup allows you to further customize your user model and admin interface while benefiting from the features provided by django-modern-user
.
Usage
With django-modern-user
and your subclassed user model, authentication is done using the email field. The email field is case-insensitive, ensuring a user-friendly authentication process.
First, ensure that you have created a subclass of ModernUser
as described in the Installation section.
Here's an example of how you might create a new user with your subclassed user model:
# Assume you have defined CustomUser in your models.py
from <your_app_name>.models import CustomUser
# Create a new user
user = CustomUser.objects.create_user(email='example@example.com', password='password123')
# Create a superuser
superuser = CustomUser.objects.create_superuser(email='admin@example.com', password='password123')
In this example, replace <your_app_name> with the name of your Django app. This way, you're creating users with your project-specific user model, which subclasses django-modern-user's ModernUser.
Custom User Manager
django-modern-user
comes with a custom user manager, ModernUserManager
, which handles user creation and ensures the email field is used for authentication.
Contributing
Feel free to fork the project, open a PR, or submit an issue if you find bugs or have suggestions for improvements.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_modern_user-1.0.0.tar.gz
.
File metadata
- Download URL: django_modern_user-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7ca236ee63c47abe722aa44cdc908c57f5577fa3c0d123b015d6dab13dddc81 |
|
MD5 | e6fb6e40fa295653bc5fd4fa799281a9 |
|
BLAKE2b-256 | eaaea519ebdf8ee8be859fc6b0a34578f322b70b3bf82a2d5086f1aaf44a20b2 |
File details
Details for the file django_modern_user-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_modern_user-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f536648723bae84486e3326ef086b3d1b4f7070a284790b3cf89a42626facbbf |
|
MD5 | e0f4d1e3639584e906333dc834b5cf30 |
|
BLAKE2b-256 | 748a917342805281f6ab6b42b9d2d410e28edc1eced519ea777bbcbebcd0d94c |