0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 11:10:43 +01:00

Support indexing ForeignKeys which point to models with custom PK type

This commit is contained in:
Karl Hobley 2018-07-20 15:16:06 +01:00
parent f67f0e39c3
commit bf2661c952
2 changed files with 16 additions and 0 deletions

View File

@ -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)

View File

@ -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