Skip to content

Commit

Permalink
Merge pull request #3849 from allegro/fix-other-m2m-polymorphic-save
Browse files Browse the repository at this point in the history
Fix other m2m polymorphic save
  • Loading branch information
hipek8 authored Sep 24, 2024
2 parents ff0c634 + 9bca2c2 commit 7b72ce4
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/ralph/deployment/migrations/0009_auto_20240924_1133.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.8 on 2024-09-24 11:33
from __future__ import unicode_literals

from django.db import migrations
import ralph.lib.polymorphic.fields


class Migration(migrations.Migration):

dependencies = [
('deployment', '0008_auto_20240705_1210'),
]

operations = [
migrations.AlterField(
model_name='preboot',
name='items',
field=ralph.lib.polymorphic.fields.PolymorphicManyToManyField(blank=True, to='deployment.PrebootItem', verbose_name='files'),
),
]
3 changes: 2 additions & 1 deletion src/ralph/deployment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ralph.lib.external_services.models import JobQuerySet
from ralph.lib.mixins.fields import NUMP
from ralph.lib.mixins.models import AdminAbsoluteUrlMixin, NamedMixin
from ralph.lib.polymorphic.fields import PolymorphicManyToManyField
from ralph.lib.polymorphic.models import Polymorphic, PolymorphicBase
from ralph.lib.transitions.models import TransitionJob

Expand Down Expand Up @@ -149,7 +150,7 @@ class Preboot(AdminAbsoluteUrlMixin, NamedMixin):
objects = Manager()
active_objects = ActiveObjectsManager()

items = models.ManyToManyField(
items = PolymorphicManyToManyField(
PrebootItem,
blank=True,
verbose_name=_('files'),
Expand Down
6 changes: 6 additions & 0 deletions src/ralph/lib/polymorphic/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.db.models import ManyToManyField


class PolymorphicManyToManyField(ManyToManyField):
def save_form_data(self, instance, data):
getattr(instance, self.attname).set(data[:])
2 changes: 1 addition & 1 deletion src/ralph/lib/polymorphic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def values_list(self, *fields, **kwargs):
flat = kwargs.pop('flat', False)
flat = kwargs.get('flat', False)
if flat:
return [getattr(obj, fields[0]) for obj in self[:]]
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.8 on 2024-09-24 11:42
from __future__ import unicode_literals

from django.db import migrations
import ralph.lib.polymorphic.fields


class Migration(migrations.Migration):

dependencies = [
('polymorphic_tests', '0003_auto_20240506_1133'),
]

operations = [
migrations.AlterField(
model_name='somem2mmodel',
name='polymorphics',
field=ralph.lib.polymorphic.fields.PolymorphicManyToManyField(related_name='some_m2m', to='polymorphic_tests.PolymorphicModelBaseTest'),
),
]
3 changes: 2 additions & 1 deletion src/ralph/lib/polymorphic/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from django.db import models

from ralph.lib.polymorphic.fields import PolymorphicManyToManyField
from ralph.lib.polymorphic.models import Polymorphic, PolymorphicBase


Expand Down Expand Up @@ -35,6 +36,6 @@ def __str__(self):

class SomeM2MModel(models.Model):
name = models.CharField(max_length=50)
polymorphics = models.ManyToManyField(
polymorphics = PolymorphicManyToManyField(
PolymorphicModelBaseTest, related_name="some_m2m"
)
4 changes: 0 additions & 4 deletions src/ralph/networks/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ class NetworkAdmin(RalphMPTTAdmin):
})
)

def save_model(self, request, obj, form, change):
form.cleaned_data['terminators'] = form.cleaned_data['terminators'][:]
super(NetworkAdmin, self).save_model(request, obj, form, change)

def get_changelist(self, request, **kwargs):
return NetworkRalphChangeList

Expand Down
21 changes: 21 additions & 0 deletions src/ralph/networks/migrations/0016_auto_20240924_1155.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.8 on 2024-09-24 11:55
from __future__ import unicode_literals

from django.db import migrations
import ralph.lib.polymorphic.fields


class Migration(migrations.Migration):

dependencies = [
('networks', '0015_auto_20211115_1125'),
]

operations = [
migrations.AlterField(
model_name='network',
name='terminators',
field=ralph.lib.polymorphic.fields.PolymorphicManyToManyField(blank=True, to='assets.BaseObject', verbose_name='network terminators'),
),
]
3 changes: 2 additions & 1 deletion src/ralph/networks/models/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
PreviousStateMixin,
TimeStampMixin
)
from ralph.lib.polymorphic.fields import PolymorphicManyToManyField
from ralph.networks.fields import IPNetwork
from ralph.networks.models.choices import IPAddressStatus

Expand Down Expand Up @@ -268,7 +269,7 @@ class Network(
default='',
)
# TODO: create ManyToManyBaseObjectField to avoid through table
terminators = models.ManyToManyField(
terminators = PolymorphicManyToManyField(
'assets.BaseObject',
verbose_name=_('network terminators'),
blank=True
Expand Down

0 comments on commit 7b72ce4

Please sign in to comment.