Skip to main content

Django UserForeignKey

PyPI version Linter and tests Codecov

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 4.2, 5.2 and 6.0 are supported (Python 3.10+).

If you need support for Django 3.2 or 4.0/4.1, please fall back to version 0.5.0.

If you need support for the insecure and deprecated Django 1.11 and/or Python 2, please fall back to version 0.3.0.

There also is a video tutorial on YouTube that shows you basic functionality of this package.

Quickstart

  1. Install the package from pypi using pip:
pip install django-userforeignkey
  1. Add django_userforeignkey to your INSTALLED_APPS within your Django settings file:
INSTALLED_APPS = [
    ...
    'django_userforeignkey',
]
  1. Add django_userforeignkey.middleware.UserForeignKeyMiddleware to your MIDDLEWARE settings like this:
MIDDLEWARE = [
    ...
    '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 Django's 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
python -m venv ./venv
source venv/bin/activate
pip install -e .
pip install Django
cd tests/user_foreign_key_testapp
python manage.py test

Metadata

Release files for django-userforeignkey 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-userforeignkey 0.6.1
File Size Uploaded
django_userforeignkey-0.6.1.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-userforeignkey 0.6.1
File Interpreter ABI Platform
django_userforeignkey-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / django_userforeignkey-0.6.1.tar.gz

Download URL django_userforeignkey-0.6.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d6f90049196356427bbb4a52f6075c97f2af92832092a6bb86d38ab7cb1f075c
BLAKE2b-256 checksum
How to use checksums
3492387a32e6791f41cb8825c1fa111af65faa74a9572a54143b51ac14caa6bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / django_userforeignkey-0.6.1-py3-none-any.whl

Download URL django_userforeignkey-0.6.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
089d9ca58636331c38e59d5297f6d74ca7fcfcf71623a8b8e4e57e73cef9a3d5
BLAKE2b-256 checksum
How to use checksums
a64599ac1853ff5fbbc12827ff000074226b87b826bc75d808db386e79fdc15c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

1 release file

0.5.0

2 release files

0.4.0

2 release files

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page