1

The model has a "content" field. When changing this model, it is necessary to look for the symbol <a in the content field and add the line rel="nofollow" into it (as if in a tag). I’m trying to do this with a function in the models.py file. Tell me how to do this ?

The save function for this model:

def save(self, *args, **kwargs):
    if (self.original_price
            and not isinstance(self.original_price, Decimal)):
        self.original_price = Decimal(self.original_price)

    if self.parent_id is not None and self.parent_id == 0:
        self.parent = None

    # Если товар не доступен для заказа, то amount_europe и
    # amount_moscow считаем что не заданы
    if self.amount_free == 0:
        self.amount_europe = None
        self.amount_moscow = None

    if self._state.adding:
        self.sort_order = 0

    if self.content:
        self.content = self.content.replace('\n', '<br>')
        if '<a ' in self.content or '<iframe ' in self.content:
            self.content = re.sub(r'<.*?>', '', self.content,
                                  flags=re.IGNORECASE)

    # Устанавливаем цену для сортировки
    self.price_ordering = self.get_price()

    super().save(*args, **kwargs)
viam0Zah
  • 25,949
  • 8
  • 77
  • 100
Ekzo
  • 103
  • 1
  • 9
  • 1
    You have to show some effort, what do you have problem with exactly, some code etc. – prajmus Jun 22 '20 at 10:14
  • 1
    Does this answer your question? [Parsing HTML using Python](https://stackoverflow.com/questions/11709079/parsing-html-using-python) – viam0Zah Jun 22 '20 at 10:58

0 Answers0