Created
August 17, 2020 14:19
-
-
Save loleg/0418353a04b74ffc8d8d2c802f9931c9 to your computer and use it in GitHub Desktop.
Example Wagtail model with multiple contacts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from wagtail.core.fields import RichTextField | |
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel | |
from wagtail.core.models import Orderable | |
from modelcluster.fields import ParentalKey | |
from modelcluster.models import ClusterableModel | |
GENDER_CHOICES = ( | |
('M', 'Male (him/his)'), | |
('F', 'Female (she/her)'), | |
('I', 'Neutral (it/its)'), | |
('?', 'Prefer not to say'), | |
) | |
class ContactItem(ClusterableModel): | |
url = models.URLField(max_length=1024, blank=True, | |
verbose_name='E-mail address') | |
email = models.EmailField(max_length=255, blank=True, | |
verbose_name='Social media or personal web link') | |
panels = [ | |
FieldPanel('url'), | |
FieldPanel('email'), | |
] | |
class Meta: | |
abstract = True | |
def __str__(self): | |
return ' '.join([self.url, self.email]) | |
class ContactPerson(Orderable, ContactItem): | |
contact = ParentalKey( | |
'connect.ContactItem', on_delete=models.CASCADE, | |
related_name='person_contact') | |
class Person(ClusterableModel): | |
last_name = models.CharField(max_length=255, | |
verbose_name='Last name') | |
given_names = models.CharField(max_length=255, blank=True, | |
verbose_name='Given name(s)') | |
title = models.CharField(max_length=255, blank=True, | |
verbose_name='Title') | |
gender = models.CharField(max_length=1, choices=GENDER_CHOICES) | |
position = models.CharField(max_length=255, blank=True) | |
biography = RichTextField(blank=True) | |
source_id = models.IntegerField(blank=True) | |
content_panels = [ | |
FieldPanel('title'), | |
FieldPanel('first_name'), | |
FieldPanel('last_name'), | |
FieldPanel('gender'), | |
FieldPanel('position'), | |
FieldPanel('biography'), | |
InlinePanel('person_contact', label='Contacts'), | |
] | |
def fullname(self): | |
return " ".join([ self.title, self.given_names, self.last_name ]) | |
def __repr__(self): | |
return self.fullname() | |
def dict(self): | |
return { | |
'id': self.id, | |
'fullname': self.fullname(), | |
'position': self.position or '', | |
'biography': self.biography or '', | |
# 'emails': (self.related_contacts or '').split(';'), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment