A simple Django app that will give you a UserForeignKey model field.
Project description
Django UserForeignKey is a simple Django app that will give you a UserForeignKey model field for Django models. This field extends a regular ForeignKey model field, and has the option to automatically set the currently logged in user on insert and/or update.
Currently, Django 2.2 (Python 3.6+) and Django 3.2 (Python 3.6+) are supported.
If you need support for the insecure and deprecated Django 1.11 and/or Python2, please fall back to version 0.3.0.
If you need support for the insecure and deprecated Django 1.8 (and possibly 1.9 and 1.10), please fall back to version 0.2.1.
There also is a video tutorial on YouTube that shows you basic functionality of this package.
Quick start
Download and install using pip install
either from PyPi
pip install django-userforeignkey
or directly from this git repo if you prefer the development version from the master branch
pip install git+https://github.com/beachmachine/django-userforeignkey.git
Add django_userforeignkey to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [
...
'django_userforeignkey',
]
Add django_userforeignkey.middleware.UserForeignKeyMiddleware to your MIDDLEWARE settings like this:
MIDDLEWARE = (
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
'django_userforeignkey.middleware.UserForeignKeyMiddleware',
)
or if you are still using the an older Django version (e.g., Django 1.8) with MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
'django_userforeignkey.middleware.UserForeignKeyMiddleware',
)
Make sure to insert the UserForeignKeyMiddleware middleware after AuthenticationMiddleware.
Example usage
Just add UserForeignKey to your model like you would with any other foreign key.
from django.db import models
from django_userforeignkey.models.fields import UserForeignKey
class MyModel(models.Model):
my_data = models.CharField(max_length=64, verbose_name="Very important data that are somehow related to a user")
user = UserForeignKey(auto_user_add=True, verbose_name="The user that is automatically assigned", related_name="mymodels")
- The UserForeignKey behaves just like a normal foreign key to the user model (using settings.AUTH_USER_MODEL), and thus also has properties such as related_name.
However, whenever an object is created by calling an authenticated view (admin, REST API, …) which contains a request.user object, the request.user object is automatically associated.
Configuration options
The configuration options are similar to Djangos DateField
auto_user Automatically sets the current user everytime the object is saved (e.g., created or updated). This is useful for last modified by information
auto_user_add Automatically sets the current user when the object is first created. This is useful for created by information
Development and Tests
git clone --recursive https://github.com/beachmachine/django-userforeignkey
cd django-userforeignkey
virtualenv -p python2 venv # or virtualenv -p python3
source venv/bin/activate
python setup.py install
pip install Django
cd tests/user_foreign_key_testapp
python manage.py test
Project details
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
Hashes for django-userforeignkey-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4be800653355ada30819c009dff2ec4439e294a4d59a31c9cb5889a52288dce9 |
|
MD5 | 9214940c66d0f79935b2fc63d042a193 |
|
BLAKE2b-256 | 237e1cb2525fe09c3d151a4b50aa81caa79b45fed57b097e8183cc2b4c9dccb5 |
Hashes for django_userforeignkey-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3771c743c62a55429678de391999d6697574b5e7a1bd3ae267f125bba45c633 |
|
MD5 | 4839cd9e34ebecc1054985ed668c7fc3 |
|
BLAKE2b-256 | 48be350e118e482371d88531e17b9002555eeb6764b0766e5e4830599dcc993b |