Skip to content

Commit

Permalink
Merge pull request #3851 from allegro/fix-get-for-polymorphic-qs
Browse files Browse the repository at this point in the history
Fix get for polymorphic queryset
  • Loading branch information
hipek8 authored Oct 1, 2024
2 parents 9737f27 + 72a7c1a commit ed326ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ralph/assets/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,14 @@ def test_get_dc_hosts_list(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 63)

def test_get_dc_host_details(self):
dc_asset = DataCenterAssetFullFactory()
VirtualServerFullFactory.create_batch(2, parent=dc_asset)
CloudHostFullFactory.create_batch(2, hypervisor=dc_asset)
url = reverse('dchost-detail', args=(dc_asset.pk,))
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_filter_by_type_dc_asset(self):
url = '{}?{}'.format(
reverse('dchost-list'),
Expand Down
6 changes: 6 additions & 0 deletions src/ralph/lib/polymorphic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ def _clone(self, *args, **kwargs):
clone._pks_order = self._pks_order.clone() if self._pks_order else None
return clone

def get(self, *args, **kwargs):
obj = super().get(*args, **kwargs)
if hasattr(obj, 'content_type'):
obj = obj.content_type.get_object_for_this_type(pk=obj.pk)
return obj

def polymorphic_select_related(self, **kwargs):
"""
Apply select related on descendant model (passed as model name). Usage:
Expand Down

0 comments on commit ed326ea

Please sign in to comment.