I join two models with select_related
and insert their content into elasticsearch with iterator()
. It seems that I cannot obtain the content of the left joined Abstract
table. How can I obtain the column paper_abstract
in the example bellow?
class Paper(models.Model):
paper_title = models.CharField(max_length=2)
class Abstract(models.Model):
paper = models.OneToOneField(Paper,
on_delete=models.CASCADE,
related_name='abstracts',
primary_key=True)
paper_abstract = models.TextField()
And the bulk_update.py
that I use with python manage.py bulk_update
.
from django.core.management.base import BaseCommand
from elasticsearch_dsl import connections
from elasticsearch.helpers import bulk
from django.db import connection
from appln.models import Paper, Abstract
class Command(BaseCommand):
help = 'Bulk indexing.'
def _document_generator(self):
for paper_abst in Paper.objects.select_related('abstracts').iterator():
yield {
'_index': 'paper_dev',
'paper_id': paper_abst.id,
'paper_title': paper_abst.title,
'paper_abstract': paper_abst.paper_abstract
}
def handle(self, *args, **kwargs):
index = 'paper_dev'
self.stdout.write(f'Bulk idexing "{index}" ...')
connection = connections.get_connection()
succeeded, _ = bulk(connection, actions=self._document_generator(), stats_only=True)
self.stdout.write(f'Updated {succeeded} successfully')