I’m new to django, I need to create a translation using the admin panel of janogo to translate this record and save it in the same model. To translate the text I use Yandex api models.py as follows:
from django.db import models from ckeditor.fields import RichTextField import requests class Translations(models.Model): text_origin = RichTextField() lang_text_origin = models.CharField(max_length=10) text_translation = RichTextField() lang_text_translation = models.CharField(max_length=10) Now I need to come up with a logic, so that when filling in the text_origin field and saving the record in the text_translation field, a translation is written. The fields lang_text_origin and lang_text_translation will be filled manually for now.
I have already written a simple text processing script in python:
import requests def get_translation(text, lang): URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate' KEY = 'trnsl.1.1........' TEXT = text r = requests.post(URL, data={'key': KEY, 'text': TEXT, 'lang': lang}) return eval(r.text)['text'] str = "Hello world!!!!" print(*get_translation(str, 'ru')) But when trying to transfer it to Django, there are difficulties. Is it possible to write a method in models.py that translates text? Or maybe there are other ways? Thank!