Django - process field before inserting / updating database

I have a django model with a text box. I use a rich text editor (nicEdit) on the admin site so that the client can easily enter markup in the field. I would like to process the contents of the field and perform a few actions before anything is inserted into the database.

For example, I want to delete garbage generated by MS Word, font tags, etc. I hope this part should be simple, but I'm not sure what needs to be redefined or hooked to make it work.

I also want to detect deleted links, load the local copy in MEDIA_ROOT and forward img src to the local image. I'm not quite sure what to do with the remote image; I thought it django.Storagemight help, but it seems like it cannot extract content from the remote URL.

Any suggestions?

+3
source share
2 answers

Delete spam, and this should be done using a custom form field.

Downloading images ... there are several ways to fix this problem.

  • If you decide to save the location of the image and the original location in the database, you must do this with a preliminary saving of the signal.
  • , . URL- URL-.
+3

, save(), :

    def save(self):
      self.NameOfTextField = myCustomCleanFunction(self.NameOfTextField)
      super(YourModelName, self).save()

, super (modelname, self).save().

, , , , clean() ValidationError().

, . , Django Python .

+9

Source: https://habr.com/ru/post/1759699/


All Articles