Home > OS >  Django - Only update FileField column without overriding other columns
Django - Only update FileField column without overriding other columns

Time:12-10

In Django, I have a model:

class List(models.Model):
    item = models.CharField(max_length=200)
    document = models.FileField(upload_to='documents/', null=True, blank=True)

Also, I have a page which is to ONLY upload a file to List.document for an existing List: enter image description here

In views.py, I have

def upload(request, item_id):
    if request.method == 'POST':
        item = List.objects.get(pk=item_id)
        form = ListForm(request.POST, request.FILES, instance=item)
        if form.is_valid():
            form.save()
            messages.success(request, 'File saved successfully.')
        else:
            messages.error(request, f'File not saved properly: {form.errors.as_data()}')
        return redirect('home')

. However, since the form doesn't include List.item, while clicking Upload, an error occurs File not saved properly: {'item': [ValidationError(['This field is required.'])]}.

The easiest way may be add List.item as a hidden element in the html:

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <div >
        <div >
            <input type="file"  name="document" />
        </div>
        <div >
            <button  type="submit">Upload</button>
        </div>
    </div>
</form>

Nevertheless, is there a better way to get over this? Maybe any options to skip overwriting existing columns?

Thanks!

CodePudding user response:

I've followed your situation, but save() works well. The followings are codes I tested. You may try this.

models.py

from django.db import models

class List(models.Model):
    item = models.CharField(max_length=200)
    document = models.FileField(upload_to='documents/', null=True, blank=True)

forms.py

from django import forms
from .models import List

class ListForm(forms.ModelForm):
    class Meta:
        model = List
        fields = ['document']

views.py

from django.shortcuts import render, redirect
from .models import List
from .forms import ListForm

# Create your views here.
def upload(request, item_id):
    if request.method == 'POST':
        item = List.objects.get(pk=item_id)
        form = ListForm(request.POST, request.FILES, instance=item)
        if form.is_valid():
            form.save()
            # messages.success(request, 'File saved successfully.')
        else:
            # messages.error(request, f'File not saved properly: {form.errors.as_data()}')
            pass
        return redirect('home')
    return render(request, 'templates/upload.html', {'form': ListForm()})

upload.html

<!-- both forms are working -->

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{form}}
    <button  type="submit">Upload</button>
</form>

<hr/>

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <div >
        <div >
            <input type="file"  name="document" />
        </div>
        <div >
            <button  type="submit">Upload</button>
        </div>
    </div>
</form>

CodePudding user response:

Instead of using

form = ListForm(request.POST, request.FILES, instance=item)

, I should employ

from django.forms import modelform_factory
ListForm_ = modelform_factory(List, fields=("document",))

to only include necessary fields (i.e., document).

Thanks.

  • Related