Why inlineformset_factory froms data not saving ? field is rendering in my html template but froms data not saving. here is my code:
models.py
class HeaderImage(models.Model):
header_image = models.ImageField()
post = models.ForeignKey(Post, on_delete=models.CASCADE)
froms.py
BlogImageFormSet = inlineformset_factory(Post, # parent form
HeaderImage, # inline-form
fields=['header_image'] ,can_delete=False, extra=1)
#views.py
class BlogUpdateView(PermissionRequiredMixin,UpdateView):
raise_exception = True
permission_required = "blog.change_post"
model = Post
template_name = "blog_update_post.html"
form_class = BlogPost
def get_success_url(self):
self.success_url = reverse_lazy('my-account')
return self.success_url
def get_context_data(self, **kwargs):
context = super(BlogUpdateView, self).get_context_data(**kwargs)
if self.request.POST:
context['form_image'] = BlogImageFormSet(self.request.POST, instance=self.object)
else:
context['form_image'] = BlogImageFormSet(instance=self.object)
return context
def form_valid(self, form):
context = self.get_context_data()
image_form = context['form_image']
if image_form.is_valid():
self.object = form.save()
image_form.instance = self.object
image_form.save()
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response(self.get_context_data(form=form))
I am not understanding why data is not saving for inlineformset_factory field.