[Fixed]-How to link foreign key in a class based view in Django

1👍

Well I think you need to override post method in your createposts view:

def post(self, request, *args, **kwargs):
    current_inspectionfile = Inspectionfile.objects.get(pk= 
                      #enter id of current file. If id is 
                      #parameter in url then use self.kwargs['file_id'], 
                      #where file_id is name of parameter in url
                      )
    new_post = posts.objects.create(inspectionfile=current_inspectionfile, 
                      #and arguments from the form
                      )
    return new_post

And Off-topic: class names in python are usually called in CamelCase in the singular. So class Post(models.Model) and class CreatePost(CreateView):

Leave a comment