From 84d336182cd4e7d38e4ace61a5210ca3974de731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szulc?= Date: Wed, 2 Oct 2024 12:09:56 +0200 Subject: [PATCH 1/3] Migrate to polymorphic manytomany field --- .../migrations/0013_auto_20241002_1122.py | 21 +++++++++++++++++++ src/ralph/operations/models.py | 3 ++- .../migrations/0010_auto_20241002_1122.py | 21 +++++++++++++++++++ src/ralph/supports/models.py | 3 ++- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/ralph/operations/migrations/0013_auto_20241002_1122.py create mode 100644 src/ralph/supports/migrations/0010_auto_20241002_1122.py diff --git a/src/ralph/operations/migrations/0013_auto_20241002_1122.py b/src/ralph/operations/migrations/0013_auto_20241002_1122.py new file mode 100644 index 0000000000..1059ca0616 --- /dev/null +++ b/src/ralph/operations/migrations/0013_auto_20241002_1122.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2024-10-02 11:22 +from __future__ import unicode_literals + +from django.db import migrations +import ralph.lib.polymorphic.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('operations', '0012_auto_20211206_1347'), + ] + + operations = [ + migrations.AlterField( + model_name='operation', + name='base_objects', + field=ralph.lib.polymorphic.fields.PolymorphicManyToManyField(blank=True, related_name='operations', to='assets.BaseObject', verbose_name='objects'), + ), + ] diff --git a/src/ralph/operations/models.py b/src/ralph/operations/models.py index 78302ae53a..7e0b713279 100644 --- a/src/ralph/operations/models.py +++ b/src/ralph/operations/models.py @@ -16,6 +16,7 @@ NamedMixin, TaggableMixin ) +from ralph.lib.polymorphic.fields import PolymorphicManyToManyField class OperationStatus(AdminAbsoluteUrlMixin, NamedMixin, models.Model): @@ -96,7 +97,7 @@ class Operation(AdminAbsoluteUrlMixin, TaggableMixin, models.Model): resolved_date = models.DateTimeField( null=True, blank=True, verbose_name=_('resolved date'), ) - base_objects = models.ManyToManyField( + base_objects = PolymorphicManyToManyField( BaseObject, related_name='operations', verbose_name=_('objects'), blank=True, ) diff --git a/src/ralph/supports/migrations/0010_auto_20241002_1122.py b/src/ralph/supports/migrations/0010_auto_20241002_1122.py new file mode 100644 index 0000000000..e2361d3907 --- /dev/null +++ b/src/ralph/supports/migrations/0010_auto_20241002_1122.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2024-10-02 11:22 +from __future__ import unicode_literals + +from django.db import migrations +import ralph.lib.polymorphic.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('supports', '0009_auto_20240506_1633'), + ] + + operations = [ + migrations.AlterField( + model_name='support', + name='base_objects', + field=ralph.lib.polymorphic.fields.PolymorphicManyToManyField(related_name='_support_base_objects_+', through='supports.BaseObjectsSupport', to='assets.BaseObject'), + ), + ] diff --git a/src/ralph/supports/models.py b/src/ralph/supports/models.py index 6298df9a5d..cd8c3fbdf1 100644 --- a/src/ralph/supports/models.py +++ b/src/ralph/supports/models.py @@ -17,6 +17,7 @@ NamedMixin, PriceMixin ) +from ralph.lib.polymorphic.fields import PolymorphicManyToManyField from ralph.lib.polymorphic.models import PolymorphicQuerySet @@ -131,7 +132,7 @@ class Support( default=None, null=True, ) - base_objects = models.ManyToManyField( + base_objects = PolymorphicManyToManyField( BaseObject, related_name='+', through='BaseObjectsSupport', From 9791949873480232f4d6596cc2978fd0948d229d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szulc?= Date: Wed, 2 Oct 2024 12:32:48 +0200 Subject: [PATCH 2/3] Change default manager for licences --- src/ralph/licences/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ralph/licences/models.py b/src/ralph/licences/models.py index d0e2383d86..527c2e5e1a 100644 --- a/src/ralph/licences/models.py +++ b/src/ralph/licences/models.py @@ -256,6 +256,7 @@ class Licence(Regionalizable, AdminAbsoluteUrlMixin, PriceMixin, BaseObject): ) ) + objects = models.Manager() polymorphic_objects = PolymorphicQuerySet.as_manager() objects_used_free = LicencesUsedFreeManager() objects_with_related = LicencesRelatedObjectsManager() From 4d0c405ecfd667ed07a77a4b8401124a88c141a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szulc?= Date: Wed, 2 Oct 2024 14:54:33 +0200 Subject: [PATCH 3/3] Use objects manager in graphs --- src/ralph/dashboards/models.py | 4 +--- src/ralph/licences/models.py | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ralph/dashboards/models.py b/src/ralph/dashboards/models.py index 8be067c4cf..4e395bafde 100644 --- a/src/ralph/dashboards/models.py +++ b/src/ralph/dashboards/models.py @@ -299,9 +299,7 @@ def _default_aggregation_handler( ) def build_queryset(self, annotated=True, queryset=None): - model = self.model.model_class() - model_manager = model._default_manager - queryset = queryset or model_manager.all() + queryset = queryset or self.model.model_class().objects.all() grouping_label = GroupingLabel(connection, self.params['labels']) if annotated: diff --git a/src/ralph/licences/models.py b/src/ralph/licences/models.py index 527c2e5e1a..d0e2383d86 100644 --- a/src/ralph/licences/models.py +++ b/src/ralph/licences/models.py @@ -256,7 +256,6 @@ class Licence(Regionalizable, AdminAbsoluteUrlMixin, PriceMixin, BaseObject): ) ) - objects = models.Manager() polymorphic_objects = PolymorphicQuerySet.as_manager() objects_used_free = LicencesUsedFreeManager() objects_with_related = LicencesRelatedObjectsManager()