Add "primary" field to Note model

This commit is contained in:
Oliver Walters 2026-05-21 07:26:28 +00:00
parent ac8e8d978c
commit ab0da25de8
2 changed files with 29 additions and 0 deletions

View File

@ -87,6 +87,14 @@ class Migration(migrations.Migration):
verbose_name="Update By",
),
),
(
"primary",
models.BooleanField(
default=False,
help_text="Is this the primary note for the associated model?",
verbose_name="Primary",
),
)
],
options={
"verbose_name": "Note",

View File

@ -2935,8 +2935,23 @@ class Note(
def save(self, *args, **kwargs):
"""Perform custom save checks before saving a Note instance."""
self.check_save()
others = Note.objects.filter(
model_type=self.model_type, model_id=self.model_id
).exclude(pk=self.pk)
# If this is the *only* note for this model instance, then set it as the primary note
if not others.exists():
self.primary = True
super().save(*args, **kwargs)
# Once this note is saved, mark other notes as non-primary
if self.primary:
Note.objects.filter(
model_type=self.model_type, model_id=self.model_id
).exclude(pk=self.pk).update(primary=False)
def delete(self):
"""Perform custom delete checks before deleting a Parameter instance."""
self.check_delete()
@ -2976,6 +2991,12 @@ class Note(
content_object = GenericForeignKey('model_type', 'model_id')
primary = models.BooleanField(
default=False,
verbose_name=_('Primary'),
help_text=_('Is this the primary note for the associated model?'),
)
title = models.CharField(
max_length=100, verbose_name=_('Title'), help_text=_('Note title')
)