is there any way to import csv file to django rest framework models?
Thank You in advance.
Using python csv you can open/read/write/modify any csv file.
Using this method, you can parse your file and save it using DRF.
I wont post any code because its a general understanding question.
Good Luck.
https://docs.python.org/2/library/csv.html
Reading a CSV file using Python
Related
I know there is an import-export package which allows us to import data from csv file to our own models by making changes in the admin.py file, but I am not able to do this if I want to import data from csv file to the default User model present in Django. Is there any way to do that?
You most likely want to write your own custom Management Command for this job.
You can import the CSV data easily with Tablib.
In the management command you have complete access to your Django models. Then you just invoke your new command with python manage.py <name_of_your_command>!
I am using Django and I want to get data via an API in CSV format and import it to my models but I don't know how to do this properly.
Please help me.
You might want to take a look at django import-export
In this only create python file in one method is read, in this method through fetch the data of magento.
How i can do this, anyone can help me ?
from magento.api import API
import warnings
with API('url','username','passward') as magento:
result=magento.call('catalog_product.info',[int(1),None,None,'id'])
print result
I am new to Django and im trying to download a string in doc/txt/csv file format.
I am not able to find the logic how to do this.
Django temple has a big string of data and i want to download it on click..
Thanks..
Here's how to output a CSV doc: https://docs.djangoproject.com/en/dev/howto/outputting-csv/
I got the solution.
I wanted to Output excel with Django and here is how we do it.
I'm new to django and been designing some basic models that contain FileFields.
Here is an example of my model:
class Sample(models.Model):
pub_date = models.DateTimeField('Publish Date', default=datetime.now)
upfile = models.FileField(upload_to='samples/')
I have tested the file upload via admin, but now I'm looking for other solutions to submit files via REST API. My first searches lead to Piston but most examples don't seem to involve models, only file upload to websites.
My objective is to parse directories, for example with os.walk, and submit the files and fill the model with the file info.
That said I'm looking for suggestions and leads in order to start investigating.
Thanks in advance!
You probably shouldn't be looking to piston for new builds anymore. It's essentially unmaintained and has been for a while now. django-tastypie and django-rest-framework are your best bets, although there's also a bunch of less fully featured frameworks cropping up.
REST framework supports standard form-encoded file uploads, see http://django-rest-framework.org/api-guide/fields.html#filefield
I'm not sure about tastypie's support for file uploads.
I've went back to the basics and decided to try creating a local script that reads calls File and the Sample Model. Since I will submit files directly from the same server, this solution immensely more simple than using a REST API, which delivers more flexibility than what I need.
This was my solution:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import sys
sys.path.append('/opt/proj')
sys.path.append('/opt/proj/web')
from django import db
from django.core.files import File
from django.utils import timezone
from web.myapp.models import Sample
filesample = File(open(sys.argv[1],'rb'))
filesample.name = os.path.basename(filesample.name)
Sample(upfile=filesample, pub_date=timezone.now()).save()
Looking back this was incredibly simple, but I hope it can help someone with the same problem.
Feel free to comment.
Thanks!