开发者

How can I extract text from an uploaded file to populate a Django model field?

I'm writing a Django application which will require a Script object which has access both to a file containing the script and the fulltext of the script. The script file is an xls file, and I have no problems in extracting the data from it or writing data to it programmatically (xlrd and friends are great!), and I'm storing it in a models.FileField. I would like to store the fulltext in a models.TextField, but my problem is that I don't know how to put the workflow together correctly so tha开发者_高级运维t the fulltext and the stored file mirror each other.

Workflows I want:

Script object created -> file uploaded to file field -> text extracted from file saved to fulltext field

and

Script fulltext edited -> text written back to file in file field -> script saved

and

Script file replaced with new file -> text extracted from file saved to fulltext field

I have been playing around with the signals framework (presave and postsave), but I don't know how to make it, er, save the changes that I am hoping for, since that would require calling save->generating an infinite loop. Also, presave seems to be invoked before the file is uploaded (using a form), which makes my server puke at me.

Any ideas?


It turns out that I was overengineering this problem. The signal I was actually looking for was post_init, and the function that I wrote is literally just

@receiver(post_save, sender=Script)
def retrieve_fulltext(sender, **kwargs):
    script = kwargs['instance']
    s = open(script.script.path)
    text = s.read()

    if script.fulltext == text:
        return else: script.fulltext = text
        script.save()

Which still, yes, requires a bit more to make it work with changes in fulltext triggering a write to the file. Sorry, SO, for asking a silly question. I hope this answer is at least useful to someone else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜