From 58a9862cd1392c0c3384ce37d4b8eeb51dd7da32 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 22 Jun 2020 14:03:59 +0100 Subject: [PATCH] Avoid extra query when serialising Template created_by MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/schemas.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/schemas.py b/app/schemas.py index c985d1812..85956e0cf 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -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):