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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…