Skip to content
Snippets Groups Projects
Commit 807d0ba6 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Avoid reloading the zone after creating an element

parent 76da7b7e
No related branches found
No related tags found
1 merge request!877Avoid reloading the zone after creating an element
......@@ -365,14 +365,19 @@ class ElementCreateSerializer(ElementLightSerializer):
# - use .only('id') to load only the id
# This works because of the unique constraint on image_id + polygon
try:
zone_id = Zone.objects.filter(
zone = Zone.objects.filter(
image_id=image.id,
polygon=polygon
).only('id').get().id
).only('id').get()
except Zone.DoesNotExist:
zone_id = image.zones.create(polygon=polygon).id
zone = image.zones.create(polygon=polygon)
# Set the zone attributes to avoid retrieving the zone, image and server again
# when the endpoint returns the created element without the slim_output mode.
zone.image = image
zone.polygon = polygon
else:
zone_id = None
zone = None
element = Element.objects.create(
corpus=validated_data['corpus'],
......@@ -380,7 +385,7 @@ class ElementCreateSerializer(ElementLightSerializer):
name=validated_data['name'],
source=validated_data.get('source'),
worker_version=validated_data.get('worker_version'),
zone_id=zone_id,
zone=zone,
)
# Make relation to parent, without dealing with children as there cannot be
......
......@@ -91,7 +91,7 @@ class TestCreateElements(FixtureAPITestCase):
name='The castle of my dreams',
image=str(self.image.id),
)
with self.assertNumQueries(19):
with self.assertNumQueries(16):
response = self.client.post(**request)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
data = response.json()
......@@ -230,7 +230,7 @@ class TestCreateElements(FixtureAPITestCase):
image=str(self.image.id),
polygon=polygon
)
with self.assertNumQueries(19):
with self.assertNumQueries(16):
response = self.client.post(**request)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
data = response.json()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment