Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

python - Django the Note instance doesn't keep the images when form is submitting?

implementing below code, I achieved this 1 Note model with many Images. the uploading_views is working fine. Multiple Uploads at the same time, with same Choose File button, & all save together.

Now i want to perform the PostUpdateViews for updating the Note. i also get the note data into the form instead images. The code of PostUpdateViews function is only update note instance [title, text] instead images. when i submit the form the form is create new images it's not changing the old images. how can i get Note related images into the Note form and update it. I would be grateful for any help.

Models

class Note(models.Model):
    title = models.CharField(max_length=30)
    text = models.TextField(null=True, blank=True)

class Images(models.Model):
    note = models.ForeignKey(Note, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='files/', null=True, blank=True)

Views

def uploading_views(request):
    if request.method == "POST":
        form = NoteFullForm(request.POST or None, request.FILES or None)

        if form.is_valid():
            title = form.cleaned_data['title']
            text = form.cleaned_data['text']
            note_obj = Note.objects.create(title=title, text=text)  # create will create as well as save too in db.
            for f in request.FILES.getlist('images'):
                Images.objects.create(note=note_obj, image=f)
            return HttpResponse('success')
    else:
        form = NoteFullForm()
    return render(request, 'uploading.html', {'form': form})


def PostUpdateViews(request, id):
    note = Note.objects.get(id=id)
    form = NoteFullForm(request.POST or None, request.FILES or None, instance=note)
    if form.is_valid():
        title = form.cleaned_data['title']
        text = form.cleaned_data['text']
        note_obj = Note.objects.create(title=title, text=text)
        for f in request.FILES.getlist('images'):
            Images.objects.create(note=note_obj, image=f)
        form.save()
        return redirect('/up/update/27/')
    else:
        form = NoteFullForm(instance=post)
    return render(request, 'uploading.html', {'form': form})
question from:https://stackoverflow.com/questions/65905570/django-the-note-instance-doesnt-keep-the-images-when-form-is-submitting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to delete the old instances of Images, also you are creating a new instance of Note for some reason! change it like so:

def PostUpdateViews(request, id):
    note = Note.objects.get(id=id)
    form = NoteFullForm(request.POST or None, request.FILES or None, instance=note)
    if form.is_valid():
        title = form.cleaned_data['title']
        text = form.cleaned_data['text']
        Images.objects.filter(note=note).delete() # Deleting old images
        for f in request.FILES.getlist('images'):
            Images.objects.create(note=note, image=f)
        form.save()
        return redirect('/up/update/27/')
    else:
        form = NoteFullForm(instance=post)
    return render(request, 'uploading.html', {'form': form})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...