Avoid extra query when serialising Template created_by

It’s a UUID column, but by default Marshmallow wants to select the id
from the users table, not from the templates table, because the two
are foreign-keyed.

Adding the property explicity like this forces it to select from the
`created_by_id` column, but still serialises it to the `created_by`
field to avoid any breaking change.
This commit is contained in:
Chris Hill-Scott
2020-06-22 14:03:59 +01:00
parent 8d61c1ef4b
commit 58a9862cd1

View File

@@ -343,7 +343,9 @@ class BaseTemplateSchema(BaseSchema):
class TemplateSchema(BaseTemplateSchema):
created_by = field_for(models.Template, 'created_by', required=True)
created_by_id = field_for(
models.Template, 'created_by_id', dump_to='created_by', dump_only=True
)
process_type = field_for(models.Template, 'process_type')
redact_personalisation = fields.Method("redact")
@@ -357,6 +359,9 @@ class TemplateSchema(BaseTemplateSchema):
if not subject or subject.strip() == '':
raise ValidationError('Invalid template subject', 'subject')
class Meta(BaseTemplateSchema.Meta):
exclude = BaseTemplateSchema.Meta.exclude + ('created_by',)
class TemplateHistorySchema(BaseSchema):