Created
July 10, 2023 20:36
-
-
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.
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
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