# Imports
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
# Exports
__all__ = (
"OwnedByModel",
)
# Constants
AUTH_USER_MODEL = settings.AUTH_USER_MODEL
# Models
[docs]class OwnedByModel(models.Model):
"""Identifies a user that owners the record, as distinct from the user that creates or modifies the record.
**Fields**
This class will add the following fields:
- ``owned_by`` The user that owns the record.
**Implementation**
.. code-block:: py
class Business(OwnedByModel):
# ...
"""
# blank must be True to allow form submission.
owned_by = models.ForeignKey(
AUTH_USER_MODEL,
blank=True,
help_text=_("Select the user that owns the record."),
related_name="%(app_label)s_%(class)s_owned_records",
null=True,
on_delete=models.SET_NULL,
verbose_name=_("owned by")
)
class Meta:
abstract = True
[docs] def set_record_owner(self, user, commit=True):
"""Set the owner of the record.
:param user: The user that owns the record.
:type user: AUTH_USER_MODEL
:param commit: Save the record.
:type commit: bool
"""
self.owned_by = user
if commit:
self.save()