I have a Django model with a text field. I'm using a rich text editor (nicEdit) on the admin site to allow the client to easily enter markup into the field. I'd 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 strip junk generated by MS Word, font tags, etc. I hope this part should be easy, but I'm not sure what to override or hook to get this working.
I also want to detect remotely-linked images, download a local copy to MEDIA_ROOT, and relink the img src to the local image. I'm not quite sure how to go about fetching the remote image; I thought django.Storage might help but it looks like it's unable to fetch content from a remote URL.
Any suggestions?
To manipulate data in your model before saving it, use the save() method like:
def save(self):
self.NameOfTextField = myCustomCleanFunction(self.NameOfTextField)
super(YourModelName, self).save()
Nothing will be saved until super(modelname, self).save() is executed.
If you want the possibility of raising some type of error instead of just processing it silently, you'll probably want to use the clean() method with raise ValidationError().
Downloading remote content is a new one for me, so I can't help you there. You might have to look past Django and find Python functions to do the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With