Usage

Creating a model

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

class MyModel(Authorship):

    pass

Saving authorship information

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

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

This will update the model’s thecut.authorship.models.Authorship.updated_by and thecut.authorship.models.Authorship.updated_at fields automatically, as well as thecut.authorship.models.Authorship.created_by and thecut.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 thecut.authorship.utils.get_website_user():

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

Tip

If you specify update_fields in your call to thecut.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 thecut.authorship.admin.AuthorshipMixin:

class MyAdmin(AuthorshipMixin, admin.ModelAdmin):

    pass

Hint

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

Integrating with class-based views and ModelForms

Use thecut.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 thecut.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 thecut.authorship.views.AuthorshipMixin on the view and thecut.authorship.forms.AuthorshipMixin on the form for this to work.

Integrating with Django REST Framework

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