Reputation: 396
Edited to clarify my meaning:
I am trying to find a method using a Django action to take data from one database table and then process it into a different form before inserting it into a second table. I am writing a kind of vocabulary dictionary which extracts data about students' vocabulary from their classroom texts. To do this I need to be able to take the individual words from the table field containing the content and then insert the words into separate rows in another table. I have already written the code to extract the individual words from the record in the first database table, I just need a method for putting it into a second database table as part of the same Django action.
I have been searching for an answer for this, but it seems Django actions are designed to handle the data for only one database table at a time. I am considering writing my own MySQL connection to inject the words into the second table in the database. I thought I would write here first though to see if anyone knows if I am missing a built-in way to do this in Django.
Upvotes: 0
Views: 940
Reputation: 396
Hubert's answer is good and got me going in the right direction, but my usage scenario required a slightly different approach. Here are relevant lines from the code I wrote for inserting processed set of words into a database table after they have been processed:
# Update or create the words and counts in the Words table.
for word, count in words.iteritems():
Word.objects.update_or_create(word=word, defaults={'count': count})
words in the function call words.iteritems() is a dictionary of containing words as keys and counts as values. word on the left side of the assignment in update_or_create() refers to the field in the database table
Using update_or_create() instead of save() means that if any editing is done to the source text of this operation at a later date, the word counts are updated in the database rather than completely new database rows being added.
Upvotes: 1
Reputation: 16494
I am pretty sure there is no built-in way for something this specific. Finding single words in a text alone is a quite complex task if you take into consideration misspelled words, hyphen-connected words, quotes, all sorts of punctuation and unicode letters.
Your best bet would be using a regex for each text and save the matches on a second model manually.
Upvotes: 1