I want to update an Article. when i try to submit the form i'm getting this error FileModel.post" must be a "Article" instance because i need to pass the article(primary key) inside FileModel(post=pk_) for create new files which is related with Article but i don't understand how can i pass the primary key from get_object function? I would be grateful for any help.
FileModel.post" must be a "Article" instance
get_object
VIEWS
class UpdateViews(UpdateView): form_class = FileForm template_name = 'update.html' success_url = '/' def get_object(self): pk_ = self.kwargs.get('pk') FileModel.objects.filter(post=pk_).delete() return get_object_or_404(Article, pk=pk_) def form_valid(self, form): form.instance.author = self.request.user for f in self.request.FILES.getlist('file'): FileModel.objects.create(post=self.get_object.pk_, file=f) # error return super().form_valid(form)
You have to use post_id=form.instance.pk or post=form.instance
post_id=form.instance.pk
post=form.instance
So:
FileModel.objects.create(post_id=form.instance.pk, file=f)
or
FileModel.objects.create(post=form.instance, file=f)
2.1m questions
2.1m answers
60 comments
57.0k users