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
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
9# Exports
11__all__ = (
12 "PrimaryModel",
13)
15# Constants
17# Models
20class PrimaryModel(PrimaryMixin):
21 """Allows a record to be marked as "primary".
23 This model supplies the following:
25 - A ``NullBooleanField`` called ``is_primary``.
26 - Two static methods for use with the ``pre_save`` signal: ``allow_one_primary()`` and ``require_one_primary()``.
28 .. code-block:: py
30 # models.py
31 from django.db import models
32 from superdjango.db.primary.models import PrimaryModel
34 class Address(models.Model):
35 # A contact's address.
37 class Contact(models.Model):
38 # A contact in an address book.
40 class LinkContactAddress(PrimaryModel):
41 # Connect contacts with addresses.
43 address = models.ForeignKey(Address)
44 contact = models.ForeignKey(Contact)
46 # receivers.py
47 from django.db.models.signals import pre_save
48 from .models import LinkContactAddress
50 pre_save.connect(
51 LinkContactAddress.allow_one_primary,
52 LinkContactAddress,
53 dispath_uid="linkcontactaddress_allow_one_primary"
54 )
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 )
63 """
65 is_primary = models.NullBooleanField(
66 _("primary"),
67 help_text=_("Indicates that this is the primary record.")
68 )
70 class Meta:
71 abstract = True