I want to monitor a folder (which receives new csv file weekly)and add that file to my django model . The purpose simply is to automize upload procedure. Even if there is a way with selenium to upload each new file (whenever arrived in the folder) that would be fine too my upload page is the default django admin page , any help would be appreciated
from django.db import models
class FilesAdmin(models.Model):
adminupload=models.FileField(upload_to='media')
title=models.CharField(max_length=50)
def __str__(self):
return self.title
Related
I am new to django and trying to create a web app to upload and download files to the google drive using gdriveapi.
I have gone through the python quickstart for Google drive and tried the code in manage.py where it is working.
How to write the same code to obtain credentials and upload and download in the django app?
How to write the view and model in django to obtain credentials and upload files to gdrive?
for my model field I use file = models.FileField(upload_to='todo', storage=gd_storage, blank=True, null=True), where todo is the app name. For the view I simply used the generic ModelViewSet, where you can just serializer.save(file=self.request.data['file'])
I have recently deployed my first django project to Heroku. My project is a Web Based Application that allows users to upload a file to the server and the server will export a product after processing it.
The views.py processing the upload process is as follows
if form.is_valid():
task= form.save(commit=False)
task.task_created_by = request.user
task.save()
The model is as follows
class db(models.Model):
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
description = models.CharField(max_length=255, blank=True)
excel = models.FileField(upload_to='task/excel/')
userinput = models.FileField(upload_to='task/userinput/')
output= models.FileField(blank=True,null=True)
uploaded_at = models.DateTimeField(auto_now_add=True)
settings.py
#Media path setting
MEDIA_URL = '/uploaded/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded')
Therefore, a part of the server involves accessing the user uploaded file and thus processing it.
I did not set up any external file storage service(e.g. aws s3) and is only using the default ephemeral file system.
when I first run the application the server response this error
No such file or directory: '/app/uploaded/task\\excel\\abc.xlsx'
However, as I am using the django model file field, when I go to the django admin panel, it shows the file is uploaded and can be downloaded from the admin panel...
Therefore, the question is where did Heroku saved the uploaded file locally...
I have tried the heroku run bash method to search for the file but with no luck...
Any help would be appreciated, many thanks!!
I have a django project in which the the form records the name and the email addresses of the users and I was able to put that data using forms.py and models.py.
What I want to do nextis to create an action through which I can download that in csv file.
My admin page looks like this now and I want to add action right above.
in order to add an action to a model in the admin page you have to create a new class like this and register it with your model:
admin.py
from youSite.views import downloadCSV
from yourSite.models import Info
class infoAdmin(admin.ModelAdmin):
actions =[downloadCSV]
admin.site.register(infoObject, infoAdmin)
You have to create the function in your views and import it into the admin page. It create a new action in that model.
Hope it helps
My Project structure:
myproject
--*
--*
--media
--*
--*
I want to allow users to access to my media directory: I want them to be able to read and download all files in my media directory and I want them to be able to write files to my media directory.
How can I accomplish this using Django rest framework?
Assume that there are 2 files in my media directory: I want to return the following JSON object as a response to a GET request:
{
file1: link_to_example1.txt
}
{
file2: link_to_example2.txt
}
How do I do this -- what should my app's model.py, views.py and maybe serializers.py look like?
We can start with simple thing like this. Every time user upload a file, one record is created in the model. If you plan to add/remove file without using your system, you may need a method to sync the latest folder content with your model's records.
class MediaFile(models.Model):
media_file = models.FileField()
uploaded = models.DateTimeField(auto_now_add=True)
class MediaFileSerializer(serializers.ListSerializer):
class Meta:
model = MediaFile
fields = ('media_file',)
class MediaViewSet(viewsets.ViewSet):
"""
A simple ViewSet for listing or retrieving MediaList.
"""
serializer_class = MediaFileSerializer
Then you can study how to upload file with Django and DRF.
I need to upload multiple files in my django app. I want to use some sightly jquery plugin such as jQuery File Upload or dropzonejs. But they are using ajax upload. It's ok when I'm download file to existing model instance but I can append ajax downloaded files in CreateView because model instance is't existing.
So I need some jquery plugin for multiple upload without ajax call or way how to save files to non-existing model instance.
My model.py
from django.db import models
class Item(models.Model):
title = models.CharField(max_length=256)
class ItemFiles(models.Model):
file = models.FileField(upload_to='item_files')
parent = models.ForeignKey(Item)