[Django]-Django UpdateView with ImageField attribute

2๐Ÿ‘

โœ…

I have a form to update Model with ImageField.
I do extend a ModelForm for my model (which is PostForm for you I guess).

But my CustomUpdateView extend UpdateView, from django generic view.

from django.views.generic.edit import UpdateView
from django.shortcuts import get_object_or_404


class CustomUpdateView(UpdateView):
    template_name = 'some_template.html'
    form_class = CustomModelForm
    success_url = '/some/url'

    def get_object(self): #and you have to override a get_object method
        return get_object_or_404(YourModel, id=self.request.GET.get('pk'))

You just have to define a get_object method and update view will update the object with value in form, but it needs to get the object you want to update.

get_object_or_404() works like a get() function on a Model, so replace id by the name of your field_id.

Hope it helps

๐Ÿ‘คBestasttung

Leave a comment