0

I'm having a lot difficulty accessing the post request data sent from my Angular server. Somehow Response(data) still returns the body of my post request accurately, however I can't access the content inside to create instance of my model. Below is my code

Angular end:

let header = new HttpHeaders({
  'Authorization': ' JWT ' + this.authToken,
  'Content-Type': 'application/json',
});

let body = {
  "var1": var1,
  "var2": var2,
};

return new Promise(resolve => {
  this.http.post(this.server, body, {headers: header, withCredentials: true}).subscribe(data => {
    console.log(data);
    resolve(data);
  }, err => {
    console.log(err);
  });
});

Django

@api_view(['POST'])
@parser_classes((JSONParser,))
def order_processing(request):
    permission_classes = (permissions.IsAuthenticated,)
    data = request.data
    item = Item.create(data["var1"])
    item.save()
    return Response(data)

and I always end up getting the error:

TypeError at /post/↵create() missing 1 required positional argument:

which I think is due to data["var1"] being unavailable. Item is my custom model.

class Item(models.Model):
    # Fields
    var1 = models.TextField(editable=False)

    # Methods
    def __str__(self):
        return self.var1

    def create(self, var1):
        order = self.create(var1=var1)
        return order

On my localhost, I tried to use httpie to request and both Querydict and request.data are empty. But then Response(data) on my deployed server is still able to return the correct body so I don't know what exactly is the problem with it.

Any help would be appreciated.

8
  • what is Item ? a Serializer ? a Model ? Commented Jul 24, 2018 at 15:17
  • It's my custom model. Commented Jul 24, 2018 at 15:18
  • create is not class method ( probably - unless you did it yourself). Try to do this : Item.objects.create(<data>) Commented Jul 24, 2018 at 15:20
  • create is a method in my model. Commented Jul 24, 2018 at 15:21
  • 1
    @Sylph there's already a create method in ModelManager, putting a create method on the instance will only confuse everyone. Better to stick to the framework's conventions. Commented Jul 24, 2018 at 15:23

1 Answer 1

1

As i mentioned in the comment - your create method is not classmethod. So you can't call it like you do. Because it can be called only on a instance.

But, the create is not going to work like you'd like.

Do this instead:

  1. remove the create method from your model
  2. in your view do this : item = Item.objects.create(var1=data['var1'])

This will create the item.

Also another thing: your permission_classes don't do anything.

Instead, import the decorator:

from rest_framework.decorators import permission_classes

And change it to:

@api_view(['POST'])
@permission_classes((permissions.IsAuthenticated,))
@parser_classes((JSONParser,))
def order_processing(request):
   ....
Sign up to request clarification or add additional context in comments.

4 Comments

The first part of your answer is correct, but the stuff about permission_classes is totally wrong; you should delete it.
@DanielRoseman hm.areyou sure? i dont see how the request can be authenticated after it enters the function ( the permission classes variable in the original impl. is not going to be used ).
You're right, now that you've removed the syntax error the code is fine.
Thank you! I've tried your implementation and it works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.