Debug School

rakesh kumar
rakesh kumar

Posted on

List down different way to use use export data in django

Export data to a CSV file:

import pandas as pd
from myapp.models import MyModel

# Retrieve data from Django models and store it in a DataFrame
queryset = MyModel.objects.all()
data = pd.DataFrame(list(queryset.values()))

# Export data to a CSV file
data.to_csv('exported_data.csv', index=False)
Enter fullscreen mode Exit fullscreen mode

Export data to an Excel file:

import pandas as pd
from myapp.models import MyModel

# Retrieve data from Django models and store it in a DataFrame
queryset = MyModel.objects.all()
data = pd.DataFrame(list(queryset.values()))

# Export data to an Excel file
data.to_excel('exported_data.xlsx', index=False)
Enter fullscreen mode Exit fullscreen mode

Export data to a JSON file:

import pandas as pd
from myapp.models import MyModel

# Retrieve data from Django models and store it in a DataFrame
queryset = MyModel.objects.all()
data = pd.DataFrame(list(queryset.values()))

# Export data to a JSON file
data.to_json('exported_data.json', orient='records')
Enter fullscreen mode Exit fullscreen mode

Export data to a SQLite database:

import pandas as pd
from myapp.models import MyModel
from django.db import connections

# Retrieve data from Django models and store it in a DataFrame
queryset = MyModel.objects.all()
data = pd.DataFrame(list(queryset.values()))

# Export data to a SQLite database
with connections['default'].cursor() as cursor:
    data.to_sql('my_table', con=cursor.connection, if_exists='replace', index=False)
Enter fullscreen mode Exit fullscreen mode

Export data to a PostgreSQL database:

import pandas as pd
from myapp.models import MyModel
from django.db import connections

# Retrieve data from Django models and store it in a DataFrame
queryset = MyModel.objects.all()
data = pd.DataFrame(list(queryset.values()))

# Export data to a PostgreSQL database
with connections['default'].cursor() as cursor:
    data.to_sql('my_table', con=cursor.connection, if_exists='replace', index=False)
Enter fullscreen mode Exit fullscreen mode

Export data to a MySQL database:

import pandas as pd
from myapp.models import MyModel
from django.db import connections

# Retrieve data from Django models and store it in a DataFrame
queryset = MyModel.objects.all()
data = pd.DataFrame(list(queryset.values()))

# Export data to a MySQL database
with connections['default'].cursor() as cursor:
    data.to_sql('my_table', con=cursor.connection, if_exists='replace', index=False)
Enter fullscreen mode Exit fullscreen mode

Export data to a MongoDB database:

import pandas as pd
from myapp.models import MyModel
from djongo import mongo_client

Retrieve data from Django models and store it in a DataFrame


queryset = MyModel.objects.all()
data = pd.DataFrame(list(queryset.values()))

# Export data to a MongoDB database
client = mongo_client.MongoClient()
db = client['my_database']
collection = db['my_collection']
collection.insert_many(data.to_dict('records'))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)