Model has no attribute _committed

后端 未结 1 1975
鱼传尺愫
鱼传尺愫 2020-12-21 12:35

I\'m using jquery form plugin http://jquery.malsup.com/form/#getting-started to upload images through ajax. When I try to upload it shows the model has no attribute _committ

相关标签:
1条回答
  • 2020-12-21 13:05

    Instead of g.background = form in the else branch, try

    try:
        g = BackgroundModel.objects.get(user=request.user)
    except BackgroundModel.DoesNotExist:
        data = form.save(commit=False)
        data.user = request.user
        data.save()
    else:
        g.background = form.save(commit=False).background
        g.save()
        # or
        BackgroundModelForm(request.POST, request.FILES, instance=g).save()
    

    Only a value of accepted types could be assigned to corresponding field of a model instance.
    models.ImageField here accepts

    • file path string
    • django.db.models.fields.files.ImageFieldFile
    • django.core.files.images.ImageFile
    0 讨论(0)
提交回复
热议问题