Issue with Django admin form: Custom file upload field affecting unrelated form field

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:

  1. 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/')
  1. 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]
  1. 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.

Welcome @imZiaRehman !

Please post the complete models and model admin classes, including all overridden methods.

Side note: That article is now 7 years old, which means it was probably written for version 2. You may need to validate the logic involved in the templates and models to ensure it’s still valid with the version you’re using.