Hide keyboard shortcuts

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 

2 

3from django.conf import settings 

4from django.db import models 

5from django.utils.translation import ugettext_lazy as _ 

6 

7# Exports 

8 

9__all__ = ( 

10 "OwnedByModel", 

11) 

12 

13# Constants 

14 

15AUTH_USER_MODEL = settings.AUTH_USER_MODEL 

16 

17# Models 

18 

19 

20class OwnedByModel(models.Model): 

21 """Identifies a user that owners the record, as distinct from the user that creates or modifies the record. 

22 

23 **Fields** 

24 

25 This class will add the following fields: 

26 

27 - ``owned_by`` The user that owns the record. 

28 

29 **Implementation** 

30 

31 .. code-block:: py 

32 

33 class Business(OwnedByModel): 

34 # ... 

35 

36 """ 

37 

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 ) 

48 

49 class Meta: 

50 abstract = True 

51 

52 def set_record_owner(self, user, commit=True): 

53 """Set the owner of the record. 

54 

55 :param user: The user that owns the record. 

56 :type user: AUTH_USER_MODEL 

57 

58 :param commit: Save the record. 

59 :type commit: bool 

60 

61 

62 """ 

63 self.owned_by = user 

64 

65 if commit: 

66 self.save()