correct search terms for reverse FK fields

This commit is contained in:
AlanCoding
2017-07-19 14:07:11 -04:00
parent 52f8579c04
commit 4bb9bd5a2b
2 changed files with 15 additions and 4 deletions

View File

@@ -10,6 +10,7 @@ import time
from django.conf import settings from django.conf import settings
from django.db import connection from django.db import connection
from django.db.models.fields import FieldDoesNotExist from django.db.models.fields import FieldDoesNotExist
from django.db.models.fields.related import OneToOneRel
from django.http import QueryDict from django.http import QueryDict
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string from django.template.loader import render_to_string
@@ -350,8 +351,11 @@ class ListAPIView(generics.ListAPIView, GenericAPIView):
if getattr(field, 'related_model', None): if getattr(field, 'related_model', None):
fields.add('{}__search'.format(field.name)) fields.add('{}__search'.format(field.name))
for rel in self.model._meta.related_objects: for rel in self.model._meta.related_objects:
name = rel.related_model._meta.verbose_name.replace(" ", "_") name = rel.related_name
if skip_related_name(name): if isinstance(rel, OneToOneRel) and self.model._meta.verbose_name.startswith('unified'):
# Add underscores for polymorphic subclasses for user utility
name = rel.related_model._meta.verbose_name.replace(" ", "_")
if skip_related_name(name) or name.endswith('+'):
continue continue
fields.add('{}__search'.format(name)) fields.add('{}__search'.format(name))
m2m_rel = [] m2m_rel = []

View File

@@ -13,9 +13,10 @@ from awx.api.generics import (
ParentMixin, ParentMixin,
SubListCreateAttachDetachAPIView, SubListAttachDetachAPIView, SubListCreateAttachDetachAPIView, SubListAttachDetachAPIView,
DeleteLastUnattachLabelMixin, DeleteLastUnattachLabelMixin,
ResourceAccessList ResourceAccessList,
ListAPIView
) )
from awx.main.models import Organization from awx.main.models import Organization, Credential
@pytest.fixture @pytest.fixture
@@ -263,3 +264,9 @@ class TestResourceAccessList:
with mocker.patch('awx.main.access.BaseAccess.can_read', mock_access): with mocker.patch('awx.main.access.BaseAccess.can_read', mock_access):
self.mock_view().check_permissions(self.mock_request()) self.mock_view().check_permissions(self.mock_request())
mock_access.assert_called_once_with(mock_organization) mock_access.assert_called_once_with(mock_organization)
def test_related_search_reverse_FK_field():
view = ListAPIView()
view.model = Credential
assert 'jobtemplates__search' in view.related_search_fields