Django create common "createObject" view logic

I've got a django app with multiple models, and mostly crud functionalities. Most of my views are very similar, ie. for example, all ModelnameCreate functions have very similar structure. So I decided to make helper function: (here is an example of helper for creating new objects)

def createFuncHelper(request, title_label , formClass , form_render  , success_render  , renderToViewWithId , success_message):
  
    form = formClass()
    if request.method=='POST':
        form = formClass(request.POST, request.FILES)
        if form.is_valid():
            form.instance.updatedBy = request.user
            newObject = form.save()
            messages.success(request, success_message)
            if(renderToViewWithId):
                return redirect(success_render, id=newObject.pk)
            else:
                return redirect(success_render) 
    return render(request, form_render, {'form':form, 'title_label':title_label})

Which is called from my each view (for each model) eg.:

def BookingCreate(request):
    return createFuncHelper(request, etc...

It works, but the question for my more experience collegues is - if it is the right approach, or are there any serious risks I'm not aware?

Back to Top