def encode_django(self, model):
        from django_elasticsearch.fields import EmbeddedModel
        if isinstance(model, EmbeddedModel):
            if model.pk is None:
                model.pk = str(uuid.uuid4())
            res = {'_app':model._meta.app_label, 
                   '_model':model._meta.module_name,
                   '_id':model.pk}
            for field in model._meta.fields:
                res[field.attname] = self.default(getattr(model, field.attname))
            res["_type"] = "emb"
            from django.contrib.contenttypes.models import ContentType
            try:
                ContentType.objects.get(app_label=res['_app'], model=res['_model'])
            except:
                res['_app'] = model.__class__.__module__
                res['_model'] = model._meta.object_name
                
            return res
        if not model.pk:
            model.save()
        return {'_app':model._meta.app_label, 
                '_model':model._meta.module_name,
                'pk':model.pk,
                '_type':"django"}
Encode ricorsive embedded models and django models
dataset/ETHPy150Open aparo/django-elasticsearch/django_elasticsearch/serializer.py/Encoder.encode_django