I have a case where users upload files and each file has certain attributes. Often times there could be 10 files that will need to be uploaded into the database with the same attributes. To save time, it would be nice to allow the user to select all ten files and have 10 records added in the database, one record per file. My models are similar to the example below:
class ContentCategory(models.Model):
name = models.CharField(max_length=100)
class Document(models.Model):
file_name = models.CharField(max_length=100, blank=True)
note = models.TextField(null=True, Blank=True)
content_category = models.ForeignKey(ContentCategory, on_delete=models.PROTECT)
document = models.FileUpload(upload_to=f'{content_category}/')
def save(self):
self.file_name = os.path.basename(self.document.name)
super(Document, self).save()
My admin.py is simple, like the below code:
class DocumentAdmin(admin.ModelAdmin):
exclude = ('file_name',)
admin.site.register(Document, DocumentAdmin)
admin.site.register(ContentCategory)
So here is an exact scenario that happens often. 10 photos will need to be uploaded and all of them get the same content category and note. Is there a way to setup the admin to allow someone to select the proper content category and write out the note and then select all 10 files to upload and have 10 records created in the document table upon save? One for each photo?