[Django]-Django form with filefield loses the file when editing an instance

4๐Ÿ‘

I have faced similar problems with file uploads in django forms. This is what I hope should help you out(provided you are willing to alter the required attribute of the archivefile field.)

if request.method == 'GET':
    if learningobject_pk:
        print learningobject_pk
        instance = LearningObject.objects.get( pk=learningobject_pk)

        print instance
        form = LearningObjectuploadform(request.POST or None, request.FILES or None, instance=instance)
        form.base_fields['archivefile'].required = False

    else:
        form = LearningObjectuploadform()
๐Ÿ‘คAnimesh Sharma

Leave a comment