I’m currently working on a Django project where I’ve extended the admin form to allow for multiple file uploads. I am following this article. However, I’m encountering an issue where my custom file upload field seems to be affecting an unrelated form field (property_title). Specifically, after implementing the custom file upload field, the value of property_title is being changed to the name of the uploaded image.
Here is simplified version of my setup:
- Models:
from django.db import models
class Property(models.Model):
property_title = models.CharField(max_length=100)
# Other fields
class PropertyImage(models.Model):
property = models.ForeignKey(Property, related_name='images', on_delete=models.CASCADE)
image = models.ImageField(upload_to='property_images/')
- Admin.py:
from django.contrib import admin
from .models import Property, PropertyImage
from .forms import PropertyAdminForm
class PropertyImageInline(admin.TabularInline):
model = PropertyImage
extra = 1
@admin.register(Property)
class PropertyAdmin(admin.ModelAdmin):
inlines = [PropertyImageInline]
- Change_form.html:
{% extends "admin/change_form.html" %}
{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
<fieldset class="module">
{% comment %} <label for="id_photos_multiple">Upload Multiple Photos:</label> {% endcomment %}
<h2>Upload Photos</h2>
<input name="photos_multiple" type="file" multiple accept="image/*" />
</fieldset>
{% endblock %}
How can I implement this setup so that I will be able to upload multiple images without effecting other fields of the form.
This issue which I figure out is that the name attribute of input field for the multiple file upload is photos_multiple, which doesn’t directly link to any field in your Property model. However, when I upload the file, the Django admin might be interpreting the input and associating it incorrectly with the property_title field due to the way the form data is being processed. I tried to make property_tile read-only. In this case it overwrite some other available field. I am unable to figure out, how to fix this issue.