Skip to content

Commit

Permalink
Merge pull request #168 from open-zaak/feature/optimize_db_quieries_i…
Browse files Browse the repository at this point in the history
…nformatieobject_related

optimize db queries - part 2 (Infromatieobject related)
  • Loading branch information
sergei-maertens committed Nov 7, 2019
2 parents 2a73e22 + 10ae2cc commit e27200b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/openzaak/components/besluiten/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ class BesluitInformatieObjectViewSet(
Verwijder een BESLUIT-INFORMATIEOBJECT relatie.
"""

queryset = BesluitInformatieObject.objects.all()
queryset = (
BesluitInformatieObject.objects.select_related("besluit", "informatieobject")
.prefetch_related("informatieobject__enkelvoudiginformatieobject_set")
.all()
)
serializer_class = BesluitInformatieObjectSerializer
filterset_class = BesluitInformatieObjectFilter
lookup_field = "uuid"
Expand Down
14 changes: 12 additions & 2 deletions src/openzaak/components/documenten/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ class GebruiksrechtenViewSet(
`null` gezet.
"""

queryset = Gebruiksrechten.objects.all()
queryset = (
Gebruiksrechten.objects.select_related("informatieobject")
.prefetch_related("informatieobject__enkelvoudiginformatieobject_set")
.all()
)
serializer_class = GebruiksrechtenSerializer
filterset_class = GebruiksrechtenFilter
lookup_field = "uuid"
Expand Down Expand Up @@ -509,7 +513,13 @@ class ObjectInformatieObjectViewSet(
endpoint bij het synchroniseren van relaties.
"""

queryset = ObjectInformatieObject.objects.all()
queryset = (
ObjectInformatieObject.objects.select_related(
"zaak", "besluit", "informatieobject"
)
.prefetch_related("informatieobject__enkelvoudiginformatieobject_set")
.all()
)
serializer_class = ObjectInformatieObjectSerializer
filterset_class = ObjectInformatieObjectFilter
lookup_field = "uuid"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 2.2.4 on 2019-11-07 10:28

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("documenten", "0008_auto_20191104_1644"),
]

operations = [
migrations.AlterModelOptions(
name="enkelvoudiginformatieobject",
options={
"ordering": ["canonical", "-versie"],
"verbose_name": "Enkelvoudige informatie object",
"verbose_name_plural": "Enkelvoudige informatie objecten",
},
),
]
4 changes: 3 additions & 1 deletion src/openzaak/components/documenten/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def __str__(self):

@property
def latest_version(self):
versies = self.enkelvoudiginformatieobject_set.order_by("-versie")
# there is implicit sorting by versie desc in EnkelvoudigInformatieObject.Meta.ordering
versies = self.enkelvoudiginformatieobject_set.all()
return versies.first()


Expand Down Expand Up @@ -317,6 +318,7 @@ class Meta:
verbose_name = _("Enkelvoudige informatie object")
verbose_name_plural = _("Enkelvoudige informatie objecten")
indexes = [models.Index(fields=["canonical", "-versie"])]
ordering = ["canonical", "-versie"]


class Gebruiksrechten(models.Model):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.test import TestCase

from ..factories import (
EnkelvoudigInformatieObjectCanonicalFactory,
EnkelvoudigInformatieObjectFactory,
)


class LastVersionTests(TestCase):
def test_canonical_last_version(self):
canonical = EnkelvoudigInformatieObjectCanonicalFactory()
eio1 = EnkelvoudigInformatieObjectFactory.create(canonical=canonical, versie=1)
eio2 = EnkelvoudigInformatieObjectFactory.create(canonical=canonical, versie=2)
eio3 = EnkelvoudigInformatieObjectFactory.create(canonical=canonical, versie=3)

self.assertEqual(canonical.latest_version, eio3)
6 changes: 5 additions & 1 deletion src/openzaak/components/zaken/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,11 @@ class ZaakInformatieObjectViewSet(
verwijderd. Consumers kunnen dit niet handmatig doen.
"""

queryset = ZaakInformatieObject.objects.all()
queryset = (
ZaakInformatieObject.objects.select_related("zaak", "_informatieobject")
.prefetch_related("_informatieobject__enkelvoudiginformatieobject_set")
.all()
)
filterset_class = ZaakInformatieObjectFilter
serializer_class = ZaakInformatieObjectSerializer
lookup_field = "uuid"
Expand Down

0 comments on commit e27200b

Please sign in to comment.