Support indexing ForeignKeys which point to models with custom PK type

pull/4481/merge
Karl Hobley 2018-07-20 15:16:06 +01:00
rodzic f67f0e39c3
commit bf2661c952
2 zmienionych plików z 16 dodań i 0 usunięć

Wyświetl plik

@ -228,6 +228,17 @@ class BaseField:
# Special case for tags fields. Convert QuerySet of TaggedItems into QuerySet of Tags
Tag = field.remote_field.model
value = Tag.objects.filter(id__in=value.values_list('tag_id', flat=True))
elif isinstance(field, RelatedField):
# The type of the ForeignKey may have a get_searchable_content method that we should
# call. Firstly we need to find the field its referencing but it may be referencing
# another RelatedField (eg an FK to page_ptr_id) so we need to run this in a while
# loop to find the actual remote field.
remote_field = field
while isinstance(remote_field, RelatedField):
remote_field = remote_field.target_field
if hasattr(remote_field, 'get_searchable_content'):
value = remote_field.get_searchable_content(value)
return value
except models.fields.FieldDoesNotExist:
value = getattr(obj, self.field_name, None)

Wyświetl plik

@ -68,3 +68,8 @@ class ConvertedValueField(models.IntegerField):
if not value:
return
return ConvertedValue(value).db_value
def get_searchable_content(self, value):
if not value:
return
return ConvertedValue(value).db_value