Рello everyone! I am writing a site on Django. I am making a search system using django-elasticsearch-dsl
.
And I have a problem when I need to index an ImageField field in the BookDocument class in order to display a book image on the book search page
Here is my Book model:
books/models.py
class Book(models.Model):
title = models.CharField(max_length=120, default=' ', verbose_name='Назва')
description = models.TextField(verbose_name='Опис')
category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='Категорія')
date_at = models.DateField(auto_now_add=True, verbose_name='Дата створення')
image = models.ImageField(upload_to='photos/%Y/%m/%d/')
authors = models.ManyToManyField(Author, verbose_name='Автори')
content = models.FileField(upload_to='contents/%Y/%m/%d/', blank=True)
price = models.IntegerField(verbose_name='Ціна', default='0')
Here is my BookDocument class:
search/documents.py
@registry.register_document
class BookDocument(Document):
image = FileField()
class Index:
name = 'books'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0
}
class Django:
model = Book
fields = [
'id',
'title',
'description',
]
but the images are not displayed on the search page, and since there is no ImageField field in django-elasticsearch-dsl, and only this file is suitable for storing file data, I do not know how to change this behavior Perhaps someone has encountered such a problem and knows how to make it work
CodePudding user response:
A FileField
[Django-doc] is essentially jus a CharField
[Django-doc] with some extra logic to wrap the data into a FieldFile
that can then open, write, etc. on the file handler.
You thus can create a document with a TextField
instead:
@registry.register_document
class BookDocument(Document):
image = fields.TextField()
class Index:
name = 'books'
settings = {'number_of_shards': 1, 'number_of_replicas': 0}
class Django:
model = Book
fields = [
'id',
'title',
'description',
]