django_extended_accounts is a Django project containing an app (extended_accounts) designed to extend the Django's default authentication system. As this is a common task, this project aims to be a reusable template to help developers saving a lot of time.
This solution is based on getting rid of the Django's User model to use a custom model called AccountModel. This model is designed to only contain authentication-related information, while all personal data resides in another model called ProfileModel, which maintains a one-to-one relationship with AccountModel. The AccountModel provided is similar to the default User model in Django, with some differences:
- It does not include first and last names; these are stored in ProfileModel.
- Upon saving, it executes an additional query to save profile information in ProfileModel.
- Accounts are initially deactivated, requiring users to activate them via email after creation.
The decision behind this design is to keep the authentication model as simple as possible and avoid interference with other apps using this solution, allowing each app to specify its own user data requirements without conflicting assumptions. However, as this is just a template, you can change this design choice if you feel it's worthy of your project.
The ProfileModel contains initially the following fields:
- First name.
- Last name.
- Phone number.
- Profile Image
Since this is a template, other fields aren't included for simplicity, but as the app is fully customizable this model may be altered to fit your project's requirements.
The provided app deals with some usual concepts present in many website accounts' system, such as:
-
Allows users to upload a profile image, which is automatically converted into WebP format for efficiency while also saving the original format. If the user updates/deletes the image or the user itself is deleted, the former is automatically removed from the server.
-
Sends a confirmation email to the user once it creates its account. If the account is not confirmed in an arbitrary period of time, the account is removed from the ddbb. This is achieved by integrating Celery into the project as a daemon.
Feel free to add/remove any functionality needed by your project.
-
Reusable apps generally shouldn't implement a custom user model as specified in Django docs. However, this app is an exception as it specifically deals with extending the user model.
-
Changing the authentication model mid-project is non-trivial so this app should only be used in new projects. Check Django docs for further information.
-
Usually, you won't interact with the ProfileModel directly but through the AccountModel. AccountModel comes with two safe methods to which you can happily pass any argument related to ProfileModel. These methods will automatically handle that data for you. In addition, these methods ensure the integrity of data if something goes wrong, as they rollback the database if saving to AccountModel or ProfileModel fails. Using other methods such as
save
orcreate
may cause undesired behavior, so use them only if you're perfectly fine with them. The safe methods are:-
create_user
: Use this method through the model manager to create a new account. Ex:Account.objects.create_user(username='johndoe', password='johndoe', email='johndoe@mail.com', phone_number=123456789)
-
update
: Use this method directly on the model instance to update an account. Ex:account.update(username='johndoe', phone_number=123456789, first_name='John')
-
If your project uses Django Rest Framework to construct an API, the DRF version may be more suitable for your needs.
This application serves as a template for extending the Django user model. It is meant to be customized, extended, or reduced according to project requirements. Since it is not published on PyPI, the code is open for editing as needed in your project. Therefore, to use it you can follow this general guidelines:
- Copy the application into your Django project.
- Add specific configurations detailed at the end of
django_extended_accounts/settings.py
to your project's settings. - Add URLs as specified in
django_extended_accounts/urls.py
. - If using Celery, add
django_extended_accounts/celery.py
to the project's main folder (wheresettings.py
resides).
For the sake of simplicity, this project uses development configurations in some tasks such as image uploading or email sending. For production projects, configurations should be adapted.
Refer to the Celery documentation (Celery Documentation) for comprehensive configuration details. The included configuration is minimal for simplicity.
If you feel that your project doesn't need Celery, you can happily remove it from the template following the next steps:
- Remove Celery from the project's requirements.
- Delete Celery configurations in
django_extended_accounts/settings.py
. - Remove
django_extended_accounts/celery.py
,extended_accounts/helpers/tasks.py
, andextended_accounts/signals/post_save_account_model.py
(and the related tests, of course).
Any contribution is more than welcome! 😸🤝🦾
We use pre-commit to ensure code formatting with Black and to run code coverage, CI checks that this's been correctly done :). Please, adhere to these standards if you want to contribute.