Usage

Creating a model

Subclass authorship.models.Authorship to create the necessary fields:

class MyModel(Authorship):

    pass

Saving authorship information

authorship.models.Authorship.save() will populate the authorship fields when necessary:

m = MyCoolModel()
m.save(user=request.user)

This will update the model’s authorship.models.Authorship.updated_by and authorship.models.Authorship.updated_at fields automatically, as well as authorship.models.Authorship.created_by and authorship.models.Authorship.created_at if the object is new.

If the model creation / update doesn’t directly relate to a user, use the site-wide generic authorship user. This can be retrieved with authorship.utils.get_website_user():

m = MyCoolModel()
m.save(user=get_website_user())

Tip

If you specify update_fields in your call to authorship.models.Authorship.save(), the list will automatically be updated to ensure that authorship information is saved.

Integrating with django.contrib.admin

You can automatically update authorship information when a model is altered in the Django admin interface by using authorship.admin.AuthorshipMixin:

class MyAdmin(AuthorshipMixin, admin.ModelAdmin):

    pass

Hint

This also applies to child inlines if they refer to subclasses of authorship.models.Authorship.

Integrating with class-based views and ModelForms

Use authorship.views.AuthorshipMixin on your django.views.generic.edit.ModelFormMixin-based views (django.views.generic.edit.CreateView, django.views.generic.edit.UpdateView.etc):

class MyModelCreateView(AuthorshipMixin, CreateView):

    form_class = MyModelForm

Then, use authorship.forms.AuthorshipMixin on your django.forms.ModelForm-based forms:

class MyModelForm(AuthorshipMixin, ModelForm):

    class Meta(object):
        model = MyModel

Together, these mixins will—upon a successful form submission—appropriately record request.user on the target object.

Warning

You must use authorship.views.AuthorshipMixin on the view and authorship.forms.AuthorshipMixin on the form for this to work.

Integrating with Django REST Framework

Use authorship.api.views.AuthorshipMixin on your CreateModelMixin / UpdateModelMixin-based API views.