Loading CSV Data in Model

74 views
Skip to first unread message

Muhammad Asim Khaskheli

unread,
Apr 29, 2021, 10:23:52 AM4/29/21
to Django developers (Contributions to Django itself)
Hi,

I wanted to ask how to load csv data into the model. What steps do we have to follow .

I have made this function but how exactly to run this code because, python manage.py loaddata wont work.

def import_data():
with open('sample-dataset.csv') as f:
reader = csv.reader(f)
for row in reader:
if row[0] != 'user_id':
_, created = Funnel.objects.get_or_create(
user_id=row[0],
event=row[1],
timestamp=row[2],
session_id=row[3]
)


Adam Johnson

unread,
Apr 29, 2021, 11:11:40 AM4/29/21
to django-d...@googlegroups.com
Hi!

I think you've found the wrong mailing list for this post. This mailing list is for discussing the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy.

For support, please follow the "Getting Help" page: https://docs.djangoproject.com/en/3.1/faq/help/ . This will help you find people who are willing to support you, and to ask your question in a way that makes it easy for them to answer.

Thanks for your understanding and all the best,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ab4f4d9d-87b6-45e4-b490-1d665b1795can%40googlegroups.com.

Franck Tchouanga

unread,
Apr 29, 2021, 11:15:28 AM4/29/21
to django-d...@googlegroups.com
import pandas as pd

data = pd.read_csv('your file', index_col=0)

On Thu, Apr 29, 2021 at 3:51 PM Franck Tchouanga <ftcho...@gmail.com> wrote:
To load a csv file in python you use  pd.read_csv('your file', index_col=0) . 

So you need to use the library pandas.

Hope it helps.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ab4f4d9d-87b6-45e4-b490-1d665b1795can%40googlegroups.com.


--
Best Wishes

Mr Tchouanga


--
Best Wishes

Mr Tchouanga

Franck Tchouanga

unread,
Apr 29, 2021, 11:15:39 AM4/29/21
to django-d...@googlegroups.com
To load a csv file in python you use  pd.read_csv('your file', index_col=0) . 

So you need to use the library pandas.

Hope it helps.

On Thu, Apr 29, 2021 at 3:23 PM 'Muhammad Asim Khaskheli' via Django developers (Contributions to Django itself) <django-d...@googlegroups.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ab4f4d9d-87b6-45e4-b490-1d665b1795can%40googlegroups.com.

Philip Mutua

unread,
Apr 30, 2021, 9:16:29 AM4/30/21
to django-d...@googlegroups.com
Hi, 
You check an Implementation I did using Django Rest framework over here hope that it will help you. I was using pandas to read the csv records to the model.  https://github.com/pmutua/drf_csv_xlsx_file_upload/blob/master/patients/views.py

Reply all
Reply to author
Forward
0 new messages