Skip to content

Instantly share code, notes, and snippets.

@cnk
Created July 10, 2023 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnk/79d5ac72cc050de2647e066c42e85096 to your computer and use it in GitHub Desktop.
Save cnk/79d5ac72cc050de2647e066c42e85096 to your computer and use it in GitHub Desktop.
I want a ManyToMany relationship between courses and departments - where the order of those mappings matters.
class CoursePage(Page, ClusterableModel):
course_number = models.CharField(
verbose_name='Course Number',
max_length=256,
help_text="Only the numeric part",
)
course_letters = models.CharField(
verbose_name='Course Letters',
max_length=256,
blank=True,
help_text="Only the letters (aka 'abc')",
)
description = RichTextField(
verbose_name='Course Description',
blank=True,
editor='course_description',
)
departments = ParentalManyToManyField('core.Department', related_name="courses", through='core.CourseDepartment')
content_panels = [
FieldPanel('title'),
InlinePanel('coursedepartments', label="Departments", min_num=1),
FieldPanel('course_number'),
FieldPanel('course_letters'),
FieldPanel('slug'),
FieldPanel('description'),
CommentPanel(),
]
# CNK we don't need the promote or publish tabs so combine everything into one content tab
edit_handler = TabbedInterface([
ObjectList(content_panels, heading='Content'),
])
class CourseDepartment(Orderable):
course = ParentalKey('core.CoursePage', related_name='coursedepartments', on_delete=models.CASCADE)
department = models.ForeignKey('core.Department', related_name='coursedepartments', on_delete=models.CASCADE)
panels = [
FieldPanel('course'),
FieldPanel('department'),
]
class Meta:
# The ordering will affect the API
ordering = ['sort_order']
@register_snippet
class Department(RevisionMixin, models.Model):
"""
Model representing a Department for the Course Catalog.
"""
abbr = models.CharField(
verbose_name='Department Abbreviation',
max_length=100
)
name = models.CharField(
verbose_name='Department Name',
max_length=100
)
is_active = models.BooleanField(
verbose_name='Is Active',
default=False,
help_text="True if department is used for the current catalog.",
)
class Meta:
verbose_name = 'Department'
ordering = ['name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment