I have been using pandas in python and I usually write a dataframe to my db table as below. I am now now migrating to Django, how can I write the same dataframe to a table through a model called MyModel? Assistance really appreciated.
# Original pandas code
engine = create_engine('postgresql://myuser:mypassword@localhost:5432/mydb', echo=False)
mydataframe.to_sql('mytable', engine,if_exists='append',index=True)
Use your own pandas code along side a Django model that is mapped to the same SQL table
I am not aware of any explicit support to write a pandas dataframe to a Django model. However, in a Django app, you can still use your own code to read or write to the database, in addition to using the ORM (e.g. through your Django model)
And given that you most likely have data in the database previously written by pandas'
to_sql
, you can keep using the same database and the same pandas code and simply create a Django model that can access that tablee.g. if your pandas code was writing to SQL table
mytable
, simply create a model like this:Now you can use this model from Django simultaneously with your existing pandas code (possibly in a single Django app)
Django database settings
To get the same DB credentials into the pandas SQL functions simply read the fields from Django settings, e.g.:
The alternative is not recommended as it's inefficient
I don't really see a way beside reading the dataframe row by row and then creating a model instance, and saving it, which is really slow. You might get away with some batch insert operation, but why bother since pandas'
to_sql
already does that for us. And reading Django querysets into a pandas dataframe is just inefficient when pandas can do that faster for us too.I'm just going through the same exercise at the moment. The approach I've taken is to create a list of new objects from the DataFrame and then bulk create them:
An example might look like this: