PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to indexes ImageField in django-elasticsearch-dsl

 October 25, 2022     django, elasticsearch, python     No comments   

Issue

Р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


Solution

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',
        ]


Answered By - Willem Van Onsem
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing