Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF/Angular: form data with nested objects with dotted/[] notation

I'm sending form data to Django Rest Framework View like this for all serialized data of a nested object named security:

------WebKitFormBoundaryAOygB8mq3Oo5I7ii
Content-Disposition: form-data; name="security[title]"

P

and it fails with 400 error saying that the field security is required.

When I'm using the DRF's web view and fill the form there, POST it I'm seeing that the notation is like this

------WebKitFormBoundaryAOygB8mq3Oo5I7ii
Content-Disposition: form-data; name="security.title"

P

As I'm using angular with ng-upload and its Upload lib (coffee):

    Upload.upload(
      url: '/services/rest/.../'+id+'/upload'
      data:
        $scope.object)

to send the form data.

Question which is correct? the dotted or [] notation? Which side to fix?

like image 468
patroqueeet Avatar asked Dec 28 '25 23:12

patroqueeet


2 Answers

found it: Upload lib can be configured by objectKey: '.k'

details: *data: {key: file, otherInfo: uploadInfo}, /* This is to accommodate server implementations expecting nested data object keys in .key or [key] format. Example: data: {rec: {name: 'N', pic: file}} sent as: rec[name] -> N, rec[pic] -> file
data: {rec: {name: 'N', pic: file}, objectKey: '.k'} sent as: rec.name -> N, rec.pic -> file */
objectKey: '[k]' or '.k' // default is '[k]'

like image 192
patroqueeet Avatar answered Dec 30 '25 12:12

patroqueeet


You Should try make your key as security.title in FormData

like image 40
Haibin Xiao Avatar answered Dec 30 '25 12:12

Haibin Xiao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!