Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4from django.db import models
5from django.utils.translation import ugettext_lazy as _
7# Exports
9__all__ = (
10 "OwnedByModel",
11)
13# Constants
15AUTH_USER_MODEL = settings.AUTH_USER_MODEL
17# Models
20class OwnedByModel(models.Model):
21 """Identifies a user that owners the record, as distinct from the user that creates or modifies the record.
23 **Fields**
25 This class will add the following fields:
27 - ``owned_by`` The user that owns the record.
29 **Implementation**
31 .. code-block:: py
33 class Business(OwnedByModel):
34 # ...
36 """
38 # blank must be True to allow form submission.
39 owned_by = models.ForeignKey(
40 AUTH_USER_MODEL,
41 blank=True,
42 help_text=_("Select the user that owns the record."),
43 related_name="%(app_label)s_%(class)s_owned_records",
44 null=True,
45 on_delete=models.SET_NULL,
46 verbose_name=_("owned by")
47 )
49 class Meta:
50 abstract = True
52 def set_record_owner(self, user, commit=True):
53 """Set the owner of the record.
55 :param user: The user that owns the record.
56 :type user: AUTH_USER_MODEL
58 :param commit: Save the record.
59 :type commit: bool
62 """
63 self.owned_by = user
65 if commit:
66 self.save()