Multiple file upload in django - python

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)

Related

automate file upload in django

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

Accessing a django model page using reverse_lazy

I have a model named Lal in Django. I have successfully registered it in my admin.py file.
Now , what I want is if a particular url is hit then, I should be able to directly redirect using reverse_lazy to the page displaying the model contents that I had registered in admin.py file.
The url that gets generated when I access the model displaying template page using my admin-login directly is:
http://127.0.0.1:8000/admin/test1_app/lal
With the help of this line of code,
url(r'yahoo/$', RedirectView.as_view(url = reverse_lazy('admin:app_list',kwargs={'app_label': 'test1_app'})), name="yahoo")
I am able to successfully generate the following url:
http://127.0.0.1:8000/admin/test1_app/
What should I add more to generate the model's url i.e. this one:
http://127.0.0.1:8000/admin/test1_app/lal
The url name for a model's changelist is admin:{{ app_label }}_{{ model_name }}_changelist. So for your app/model you want:
reverse_lazy('admin:test1_app_lal_changelist')
See the docs on reversing admin urls for more info.

Django Rest framework: I want my GET response to be a JSON object of links to all files in my media directory on my local machine. How do i do this?

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.

How to edit formset that has multiple forms inside related via generic foreign to the model of the current edit view?

In my recent project I had to create a generic model with a single file field, because the idea was to use multiple file uploads across multiple system models.
For this I created this generic model with generic foreign key and in the add view I create a formset with the extra = 1 field and in the template via jquery I add forms inside my formset.
In addition it works quietly, but I'm not able to adjust my edit view.
In my edit view I try it:
ModelGenericFormset = modelformset_factory(
ModelsFileGeneric,
form=AddModelsGenericForm
)
query_files = ModelsFileGeneric.objects.filter(
object_id=model_related_to_generic_model_with_file_field.id,
)
files = ModelGenericFormset(queryset=query_files)
and in request.post:
files_form_set = ModelGenericFormset(
request.POST,
request.FILES
)
for file_form in files_form_set:
file = file_form.save(commit=False)
setattr(
'upload_destination',
'path_to_upload'
)
file.content_object = model_related_to_generic_model_with_file_field
file.save()
An observation:
As this model where the file field is is an extremely generic model (for this reason the use of the generic foreign key), I also need at runtime (in the save in view after the post) to change the upload_to attribute of the field (this already Was fixed and works ok).
I make this edition of the "upload_to" attribute because depending on the model to which I am sending multiple files, it will vary the path from where the files will be saved.
But in save of the edit view this error occurs:
The ModelsFileGeneric could not be changed because the data didn't validate.
And the error is in:
file = file_form.save(commit=False)
I don't know what to do anymore. Thanks!

400 error on django filefield model save

I'm using django 1.6.5 on python 2.7, and am trying to upload images with a form. My MEDIA_URL is '/uploads', MEDIA_ROOT='...../projectroot/uploads'.
My model:
class Picture(models.Model):
person = models.ForeignKey(User)
image = models.ImageField(upload_to='/images')
My Form field:
images = forms.ImageField(widget=forms.FileInput(), required=False)
My form saving:
image = self.cleaned_data['images']
picture = Picture(park=model, image=image)
picture.save()
I have tried setting the uploads folder permissions to 777 recursively, so django definitely has write access, but that didn't work. Somebody said the enctype on the form mattered, so I've set that to "multipart/form-data".
If it helps, only the actual saving creates the error (although that may have something to do with django just using lazy functions)

Categories