Skip to main content

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

Project description

https://travis-ci.org/beachmachine/django-userforeignkey.svg?branch=master https://img.shields.io/pypi/v/django-userforeignkey.svg?maxAge=2592000:target:

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 1.11 (Python 2.7, Python 3.4+), Django 2.0 (Python 3.4+) and Django 2.1 (Python 3.5+) are supported.

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

  1. Download and install using pip install
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
  1. Add django_userforeignkey to your INSTALLED_APPS setting like this:
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',
)

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

Changelog

0.3.0 (Meta release, no actual code changes)

  • Dropped support for Django 1.8, 1.9 and 1.10
  • Added support for Django 2.1

0.2.1

  • Added setup.cfg with the license_file keyword, ensuring that the actual LICENSE file is also installed when using pip install
  • Improved .travis.yml

0.2.0

  • Tested for Django 2.0 support
  • Updated test app for Django 2.0 support
  • Updated tox and travis for automated tests with Django 2.0
  • Behaviour change: Prior to 0.2.0 the UserForeignKey field had editable set to False only if auto_user == True. Since 0.2.0 editable is set to False if auto_user == True or auto_user_add == True

0.1.2

  • Initial Release on PyPi

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

You can also use tox for testing, as it will test against several Django and Python versions automatically. See tox.ini for details.

The test source code is in another repository in the master branch. Changes within that repository can be fetched by calling

git submodule update --remote

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django-userforeignkey-0.3.0.tar.gz (5.9 kB) Copy SHA256 hash SHA256 Source None Aug 7, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page