TLDR: if your model saves are slow check the types
I'm new to python/django and made a stupid mistake. I have a model with author = models.TextField() and to create it I passed in thirdpartyclass.author.
It worked but was crazy slow. Like 1-5 seconds per model save. 500 saves just didn't ever finish.
What I tried:
1. Maybe its sqlite3, I'll upgrade to postgres
2. Maybe the queries are slow, enable postgres logging and see sensible inserts taking 8ms
3. Make absolutely sure it's the .save by measuring time elapsed.
4. Create a new model and add fields till the slow down happens - it never happens because I used fake data
5. Try explicitly casting everything to the correct data type before creating the model object - success
I have no idea how the django object relation mapper works but it was just choking on this third party object that I thought was a string. Calling str(thirdpartyclass.author) gave me a 1000x speedup.
I am also inexperienced but since no one has said anything yet, I have heard that most of the time it supposed to do with the database and not necessarily Django itself. Do you have the code? I’m not sure there will be more to do on the django side.
That’s certainly an interesting problem. What type of object is `thirdpartyclass.author`? It’s odd that calling `str()` on it would improve performance because surely that’s what `TextField` does internally during DB prep.
Alt title: make sure the object you are saving isn’t a promise that has to be resolved before the save can complete.