Reputation: 31
I am using DRF haystack for search with elasticsearch backend.
Notes can have 0...n Photo.
is it possible to include all the photographs in search result. or return entire json of Note object?
here are my model
class Note(activity.Activity, geoModels.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
created_at = geoModels.DateTimeField(auto_now_add=True)
title = models.CharField(_('title'),
max_length=50)
story = models.TextField(_('userStroy'),
unique=False,
help_text= ('story'))
class Photo(activity.Activity,geoModels.Model):
created_at = geoModels.DateTimeField(auto_now_add=True)
image = geoModels.ImageField(_('image'),
max_length=IMAGE_FIELD_MAX_LENGTH,
upload_to=get_storage_path)
trip_note = geoModels.ForeignKey(TripNote,
null=True,
blank=True,
related_name="photos",
verbose_name=_('tripnote'))
Upvotes: 0
Views: 298
Reputation: 51
I know this is a pretty old question, but the way I've traditionally done this with regular DRF is to create a serializer for Note, and one for Photo (Assuming that a photo can have multuple notes, as the models seem to indicate).
Inside the Photo serializer, you'd denote trip_note = NoteSerializer(many=True).
Then, using the drf-haystack documentations as a guide (https://drf-haystack.readthedocs.io/en/latest/10_tips_n_tricks.html#reusing-model-serializers), you can reuse this serializer instad of a pure drf-haystack serializer, which would look something like:
class SearchSerializer(HaystackSerializerMixin, PhotoSerializer):
class Meta(PhotoSerializer.Meta):
search_fields=("whatever you search by",)
Upvotes: 1