django - “Incorrect type. Expected pk value, received str” error

前端 未结 2 870
青春惊慌失措
青春惊慌失措 2021-01-11 10:23

I my django-rest-framework I have the following models:

Basically every ride has one final destination and can have multiple middle destinations.

mod

相关标签:
2条回答
  • 2021-01-11 11:12

    Angular/Django Rest Framework

    I had similar problem when trying to post a formdata that has a field with some array values. It turned out that I was using a wrong content-type in the post headers of angular frontend. All I needed was comment out Content-Type.

      Post(endpoint, postData) {
        let auth = `Bearer ${this.token}`;
        let headers = new HttpHeaders({
          // "Content-Type": "application/x-www-form-urlencoded",
          Authorization: auth
        });
        let fullurl = `${this.baseUrl}${endpoint}`;
       .....
      ....
    }
    
    0 讨论(0)
  • 2021-01-11 11:28

    The issue is that you are passing the name of the related Destination object into the serializer, instead of passing the pk/id of the Destination object. So Django REST framework is seeing this and complaining, because it can't resolve LA into an object.

    It sounds like you may actually be looking for a SlugRelatedField, which allows you to identify objects by a slug (LA in this case) instead of their primary keys.

    0 讨论(0)
提交回复
热议问题