Ask Your Question
3

What is the process for uploading multiple files in Django admin models?

asked 2022-09-21 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-05-25 00:00:00 +0000

huitzilopochtli gravatar image

To allow users to upload multiple files in Django admin models, you can follow these steps:

  1. In your models.py file, create a model field for your files using the FileField or ImageField class. Make sure to set the 'upload_to' attribute to specify where the files should be saved.

    Example:

    from django.db import models
    
    class MyModel(models.Model):
        files = models.FileField(upload_to='uploads/')
    
  2. In your admin.py file, create a form for your model using the ModelForm class. Set the widget for the file field to FileInputMultiple.

    Example:

    from django import forms
    from django.contrib import admin
    from django.forms import FileInputMultiple
    
    from .models import MyModel
    
    class MyModelForm(forms.ModelForm):
        class Meta:
            model = MyModel
            fields = ['files']
            widgets = {
                'files': FileInputMultiple()
            }
    
    class MyModelAdmin(admin.ModelAdmin):
        form = MyModelForm
    
    admin.site.register(MyModel, MyModelAdmin)
    
  3. Make sure to include the form field in your admin model fields list so the form is displayed in the admin interface.

    Example:

    class MyModelAdmin(admin.ModelAdmin):
        form = MyModelForm
        list_display = ['id', 'files']
        fields = ['files']
    

With these steps, your users will now be able to upload multiple files using the Django admin interface.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-09-21 11:00:00 +0000

Seen: 17 times

Last updated: May 25 '21