django ugettext_lazy не работает внутри сохранения моделей. Модель - PullRequest
0 голосов
/ 26 октября 2019

Это моя модель:

class Friend(AbstractBaseModel):
    """
        Table for the friendship of the pets
    """
    STATE_CHOICES = Choices(
         (0, 'accepted',  _('accepted')),
         (1, 'requested', _('requested')),
    )

    source = models.ForeignKey('pets.Pet', on_delete=models.CASCADE,
                               verbose_name=_('original pet'), help_text=_('the pet that the friendship comes from'),
                               related_name="%(app_label)s_%(class)s_source_items")
    reciever = models.ForeignKey('pets.Pet', on_delete=models.CASCADE,
                                 verbose_name=_('reciever pet'), help_text=_('the pet that the friendship comes at'),
                                 related_name="%(app_label)s_%(class)s_reciever_items")
    state = models.IntegerField(default=0, null=True, blank=True, choices=STATE_CHOICES,
                                verbose_name=_('state of the friendship'))

    history = HistoricalRecords()

    class Meta:
        verbose_name = _('Friend')
        verbose_name_plural = _('Friends')
        unique_together = ['source', 'reciever']

    def __str__(self):
        return f'{self.source} friendship with {self.reciever}'

    def _the_friendship_petition_is_newly_created(self):
        return self.id is None and self.state == self.STATE_CHOICES.requested

    def _the_friendship_is_accepted(self):
        return self.id is not None and self.state == self.STATE_CHOICES.accepted

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if self._the_friendship_petition_is_newly_created():
            super().save(force_insert, force_update, using, update_fields)

            pet_creator = get_object_or_404(RelationshipUserPet.objects.filter(creator=True), pet_id=self.reciever_id)
            content_of_title = _(f'Your pet {self.reciever} has recieved a friendship invitation')
            title = (content_of_title[:47] + '..') if len(content_of_title) > 47 else content_of_title
            WebNotification.objects.create(title=title,
                                           description=_(f'Your pet {self.reciever} has recieved a friendship invitation of {self.source}'),
                                           # TODO: this url notification needs to change
                                           url=WebNotificationRoute.objects.get(
                                               fix_id=WebNotificationRoute.FIX_ID_CHOICES.friendship_accept
                                           ),
                                           user_id=pet_creator.user.id,
                                           params=f'{{"friendship": {self.id}, "source": {self.source_id},'
                                           f'"reciever": {self.reciever_id}}}',
                                           read=False)

Это мой django.po

#: apps/friends/models.py:54
#, fuzzy, python-brace-format
#| msgid "Your pet {self.reciever} has recieved a friendship "
msgid ""
"Your pet {self.reciever} has recieved a friendship invitation of {self."
"source}"
msgstr ""
"Tu mascota {self.reciever} ha recibido una invitación de amistad de {self."
"source}"

Когда я сохраняю дружбу, уведомление отправляется, но сообщение:

Ваш домашний питомец, созданный обновлением api view api получил приглашение дружбы Начо Гутьерреса

Но мне нужно быть:

Создан питомец Tu mascotaпо обновлению api view api ha recibido una invitación de amistad de Nacho Gutierrez

Я уже проверял, что он переводит, но не эти сообщения. Любая помощь будет оценена.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...