How to update an index that is mix of multiple data models

Hi,

We have a model order. This is having a reference to different table fulfullment. I need to fetch details from both table for indexing. I achieved the get part of index by overriding get_raw_record() function. but I am not able to figure out that how update will work. ie wherever there is change in fulfullment then , then Order Index should update.

# from index.py file 
@register(Order)
class OrderIndex(AlgoliaIndex):
    custom_objectID = 'id'
    index_name = 'orders'
    fields = (
        'name', 'created_at', 'total_price', 'fulfillment_status', 'email', 'tags', 'item_names')

    def get_raw_record(self, instance, update_fields=None):
        algolia_dict = instance.algolia_fields
        res_dict = super(OrderIndex, self).get_raw_record(instance, update_fields)
        return merge_two_dicts(res_dict, algolia_dict)

# Order and Fulfillment models:

class Order(TimeStampedModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    retailer = models.ForeignKey(Retailer, on_delete=models.CASCADE)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    oid = models.BigIntegerField()
    email = models.CharField(max_length=100)
    closed_at = models.DateTimeField(blank=True, null=True)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()
    no = models.CharField(max_length=100)
    test = models.BooleanField(default=False)
    total_price = models.FloatField(blank=True, null=True)
    currency = models.CharField(max_length=10, blank=True, null=True)
    name = models.CharField(max_length=50)
    fulfillment_status = models.CharField(max_length=20, blank=True, null=True)
    tags = models.CharField(max_length=200, blank=True, null=True)
    shipping_address = JSONField(default=dict)
    customer_info = JSONField()

    @property
    def algolia_fields(self):
        fulfillmemt_ids = [item.id for item in self.order_fulfillments.all()]
        return {'customer_info': {
            'first_name': self.customer.first_name,
            'last_name': self.customer.last_name,
            'email': self.customer.email,
            'phone': self.customer.phone

        },
            'created_date': arrow.get(self.created).to('UTC').datetime.strftime('%Y%m%d'),
            'viewable': [self.customer.id, self.retailer.id],
            'fulfillmemt_ids': fulfillmemt_ids
        }

 
    class Meta:
        db_table = 'order'


class Fulfillment(TimeStampedModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='order_fulfillments')

Hi @girish,

Indeed with Algolia you will need to nest Fultillments under Order, and have only on index.

I checked online and it seems like the best solution would be to implement a custom save method for Fulfillment that calls Order.save. You could also connect to your Order’s post_save or pre_save signal.
this is based on I read here, which makes sense to me but I have confess, I’m not a Django expert

Please let me know if that works for you!