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 

3 

4from django.db import models 

5from django.utils.translation import ugettext_lazy as _ 

6# from superdjango.shortcuts import there_can_be_only_one 

7from .mixins import PrimaryMixin 

8 

9# Exports 

10 

11__all__ = ( 

12 "PrimaryModel", 

13) 

14 

15# Constants 

16 

17# Models 

18 

19 

20class PrimaryModel(PrimaryMixin): 

21 """Allows a record to be marked as "primary". 

22 

23 This model supplies the following: 

24 

25 - A ``NullBooleanField`` called ``is_primary``. 

26 - Two static methods for use with the ``pre_save`` signal: ``allow_one_primary()`` and ``require_one_primary()``. 

27 

28 .. code-block:: py 

29 

30 # models.py 

31 from django.db import models 

32 from superdjango.db.primary.models import PrimaryModel 

33 

34 class Address(models.Model): 

35 # A contact's address. 

36 

37 class Contact(models.Model): 

38 # A contact in an address book. 

39 

40 class LinkContactAddress(PrimaryModel): 

41 # Connect contacts with addresses. 

42 

43 address = models.ForeignKey(Address) 

44 contact = models.ForeignKey(Contact) 

45 

46 # receivers.py 

47 from django.db.models.signals import pre_save 

48 from .models import LinkContactAddress 

49 

50 pre_save.connect( 

51 LinkContactAddress.allow_one_primary, 

52 LinkContactAddress, 

53 dispath_uid="linkcontactaddress_allow_one_primary" 

54 ) 

55 

56 # Or to require a primary record. 

57 pre_save.connect( 

58 LinkContactAddress.require_one_primary, 

59 LinkContactAddress, 

60 dispath_uid="linkcontactaddress_require_one_primary" 

61 ) 

62 

63 """ 

64 

65 is_primary = models.NullBooleanField( 

66 _("primary"), 

67 help_text=_("Indicates that this is the primary record.") 

68 ) 

69 

70 class Meta: 

71 abstract = True