Skip to main content

A simple Django app that will give you a UserForeignKey model field.

Project description

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 2.2 (Python 3.7+) and Django 3.2 (Python 3.7+) are supported.

If you need support for the insecure and deprecated Python 3.6, please fall back to version 0.4.0.

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.

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

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

django-userforeignkey-0.5.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

django_userforeignkey-0.5.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file django-userforeignkey-0.5.0.tar.gz.

File metadata

  • Download URL: django-userforeignkey-0.5.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for django-userforeignkey-0.5.0.tar.gz
Algorithm Hash digest
SHA256 fbf899c15f2f04fc9cc37f0d104d6eefb60785173702d94563465605bae3faaa
MD5 023902a7f0af58cac6baba4d8173b774
BLAKE2b-256 b0ae15f796f5d2128375da85ca3cbf61b979e0a3b53fac1ec2bcbfd1e200df0f

See more details on using hashes here.

File details

Details for the file django_userforeignkey-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_userforeignkey-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4690634b3e25c8c4d78c570d012f347d9d7c53177c496238a6141dbff9f28a44
MD5 eaa8baaa1a02e0e57cef74e5f331639b
BLAKE2b-256 31ced0ed8b8b082467c2b79e7bbb998aeb85e4ba7a793950378764344b98677c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page