Я перевел поля моей модели с помощью django -модельтрансляции и реализовал поиск с использованием django -elasticsearch-dsl .
Проблема: django -modeltranslation создает поля перевода в БД, а не в моих моделях, и поиск работает только для полей, созданных моделью. Поскольку django -elasticsearch-dsl проверяет Модели на перестроение поискового индекса.
Когда я пытаюсь:
python3 manage.py search_index --rebuild
Я получаю сообщение об ошибке:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/apps.py", line 14, in ready
self.module.autodiscover()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/__init__.py", line 11, in autodiscover
autodiscover_modules('documents')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/barbos/django-symbolsite/symbolgraph/search/documents.py", line 7, in <module>
class SymbolDocument(Document):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/registries.py", line 65, in register_document
field_instance = document.to_field(field_name, django_field)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/documents.py", line 142, in to_field
"to an Elasticsearch field!".format(field_name)
django_elasticsearch_dsl.exceptions.ModelFieldNotMappedError: Cannot convert model field name_ru to an Elasticsearch field!
Oleh-MacSymbol-Pro:symbolgraph barbos$ python3 manage.py search_index --rebuild
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/documents.py", line 138, in to_field
model_field.__class__](attr=field_name)
KeyError: <class 'modeltranslation.fields.field_factory.<locals>.TranslationFieldSpecific'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/apps.py", line 14, in ready
self.module.autodiscover()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/__init__.py", line 11, in autodiscover
autodiscover_modules('documents')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/barbos/django-symbolsite/symbolgraph/search/documents.py", line 7, in <module>
class SymbolDocument(Document):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/registries.py", line 65, in register_document
field_instance = document.to_field(field_name, django_field)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django_elasticsearch_dsl/documents.py", line 142, in to_field
"to an Elasticsearch field!".format(field_name)
django_elasticsearch_dsl.exceptions.ModelFieldNotMappedError: Cannot convert model field name_ru to an Elasticsearch field!
Django 3.0.5 django -модельтрансляция & django -elasticsearch-dsl - последние версииasticsearch-7.6.2 db.sqlite3 (I собираюсь перейти на PostgreSQL)
связанные django файлы:
#settings.py:
INSTALLED_APPS = [
'modeltranslation',
'django_elasticsearch_dsl',
'search',
'symbol',
'category',
'homepage',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
ELASTICSEARCH_DSL={
'default': {
'hosts': 'localhost:9200'
},
}
---
#translation.py
from modeltranslation.translator import translator, TranslationOptions
from .models import Symbol
class SymbolTranslationOptions(TranslationOptions):
fields = ('name', 'content', 'meaning', 'tag',)
translator.register(Symbol, SymbolTranslationOptions)
---
#models.py
from django.db import models
from django.db.models.constraints import UniqueConstraint
class Symbol(models.Model):
id = models.IntegerField(primary_key=True)
slug = models.SlugField(unique=True) #uniqe
codepoint = models.CharField(max_length=255, unique=True) #this list should be in separete table
symbol = models.CharField(max_length=50, unique=True)
name = models.CharField(max_length=255, unique=True)
category = models.ForeignKey('Category', on_delete=models.CASCADE)
subcategory = models.ForeignKey('Subcategory', on_delete=models.CASCADE)
show_in_category = models.BooleanField()
popular = models.BooleanField(blank=True, unique=False, default=False)
tag = models.CharField(max_length=255,blank=True) #this list should be in separete table
content = models.TextField(blank=True)
meaning = models.TextField(blank=True)
imgslug = models.CharField(unique=False, max_length=255, blank=True) #uniqe
# name_en = models.CharField(max_length=255, unique=True, null=True)
# name_ru = models.CharField(max_length=255, unique=True, null=True)
# tag_en = models.CharField(max_length=255,blank=True, null=True)
# tag_ru = models.CharField(max_length=255,blank=True, null=True)
# content_en = models.TextField(blank=True, null=True)
# content_ru = models.TextField(blank=True, null=True)
# meaning_en = models.TextField(blank=True, null=True)
# meaning_ru = models.TextField(blank=True, null=True)
def __str__(self):
return self.name
---
#documents.py
from django_elasticsearch_dsl import Document
from django_elasticsearch_dsl.registries import registry
from symbol.models import Symbol
@registry.register_document
class SymbolDocument(Document):
class Index:
# Name of the Elasticsearch index
name = 'symbols'
# See Elasticsearch Indices API reference for available settings
settings = {'number_of_shards': 1,
'number_of_replicas': 0}
class Django:
model = Symbol # The model associated with this Document
# The fields of the model you want to be indexed in Elasticsearch
fields = [
'symbol',
'name',
'tag',
'content',
'meaning',
'shortcode',
'codepoint',
'slug',
'id',
#
# 'name_ru',
# 'name_uk',
# 'tag_ru',
# 'tag_uk',
# 'content_ru',
# 'content_uk',
# 'meaning_ru',
# 'meaning_uk',
# 'also_called_ru',
# 'also_called_uk',
]
Я нашел один плохой способ заставить поиск работать на других языках:
- Отключить django -модель-перевод в УСТАНОВЛЕННЫХ ПРИЛОЖЕНИЯХ.
- Добавить все поля, созданные в БД с помощью django -модель-перевода в модели.
- Добавить эти поля в documents.py в поиске приложение для создания Index для Elasti c и перезапуска django server
- Проверьте, возвращает ли "$ python3 manage.py makemigrations": "Изменения не обнаружены".
- $ python3 manage.py search_index --rebuild
- Отменить шаги 3, 2, 1 и перезапустить django сервер
Мне нужно включить переводы в t Эластичные поисковые индексы. Как новичок в разработке, я не знаю, что делать дальше.
У меня есть две идеи (больше похоже на направление), но я не знаю, как реализовать:
- Измените django -elasticsearch-dsl на индекс из БД напрямую.
- Измените django -модель-перевод, чтобы можно было добавлять поля, которые он создает в БД, в модель.