django - "Incorrect type. Expected pk value, received str" 错误
django - "Incorrect type. Expected pk value, received str" error
我的 django-rest-framework 我有以下模型:
基本上每次行程都有一个最终目的地,可以有多个中间目的地。
models.py:
class Destination(models.Model):
name=models.CharField(max_length=30)
class Ride(models.Model):
driver = models.ForeignKey('auth.User', related_name='rides_as_driver')
destination=models.ForeignKey(Destination, related_name='rides_as_final_destination')
leaving_time=models.TimeField()
num_of_spots=models.IntegerField()
passengers=models.ManyToManyField('auth.User', related_name="rides_as_passenger")
mid_destinations=models.ManyToManyField(Destination, related_name='rides_as_middle_destination')
serializers.py - RideSerializer
class RideSerializer(serializers.ModelSerializer):
driver = serializers.ReadOnlyField(source='driver.user.username')
class Meta:
model = Ride
fields = ('driver', 'destination', 'leaving_time',
'num_of_spots', 'passengers', 'mid_destinations')
read_only_fields = ('driver', 'passengers', 'mid_destinations')
问题是 - 当我尝试 POST 到 /rides/ 以添加行程时 - 例如 {destination=LA, leaving_time=19:45, num_of_spots=4}
我收到错误 "destination":["Incorrect type. Expected pk value, received str."]}
几个问题:
这是什么错误?如果我在 Ride 模型中有一个目的地作为外键,这是否意味着我要添加的目的地必须已经在 Destinations table?
如何解决这个错误?
问题是您将相关 Destination
对象的 name
传递给序列化程序,而不是将 pk
/id
传递给序列化程序11=] 对象。所以 Django REST 框架看到了这一点并抱怨,因为它无法将 LA
解析为对象。
听起来您可能真的在寻找 a SlugRelatedField
,它允许您通过 slug(在本例中为 LA
)而不是它们的主键来识别对象。
Angular/Django 休息框架
我在尝试 post 具有包含一些数组值的字段的表单数据时遇到了类似的问题。原来我在 angular 前端的 post headers 中使用了错误的 content-type。我所需要的只是注释掉 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}`;
.....
....
}
每当我们尝试将外键引用添加到实例时,Django 和 Django Rest Framework 之间存在细微差别
假设我们要将用户引用添加到另一个数据库 Table
Django
在 Django 中,我们引用用户的整个实例,如下所述
user = request.user
Django Rest 框架
但是在 DRF 中,如果我们遵循相同的过程,我们会得到类似
的错误
Incorrect type. Expected pk value, received str/User type
为了克服这个错误,我们需要引用table的主键来代替(这里id是PK)
user = request.user.id
我的 django-rest-framework 我有以下模型:
基本上每次行程都有一个最终目的地,可以有多个中间目的地。
models.py:
class Destination(models.Model):
name=models.CharField(max_length=30)
class Ride(models.Model):
driver = models.ForeignKey('auth.User', related_name='rides_as_driver')
destination=models.ForeignKey(Destination, related_name='rides_as_final_destination')
leaving_time=models.TimeField()
num_of_spots=models.IntegerField()
passengers=models.ManyToManyField('auth.User', related_name="rides_as_passenger")
mid_destinations=models.ManyToManyField(Destination, related_name='rides_as_middle_destination')
serializers.py - RideSerializer
class RideSerializer(serializers.ModelSerializer):
driver = serializers.ReadOnlyField(source='driver.user.username')
class Meta:
model = Ride
fields = ('driver', 'destination', 'leaving_time',
'num_of_spots', 'passengers', 'mid_destinations')
read_only_fields = ('driver', 'passengers', 'mid_destinations')
问题是 - 当我尝试 POST 到 /rides/ 以添加行程时 - 例如 {destination=LA, leaving_time=19:45, num_of_spots=4}
我收到错误 "destination":["Incorrect type. Expected pk value, received str."]}
几个问题:
这是什么错误?如果我在 Ride 模型中有一个目的地作为外键,这是否意味着我要添加的目的地必须已经在 Destinations table?
如何解决这个错误?
问题是您将相关 Destination
对象的 name
传递给序列化程序,而不是将 pk
/id
传递给序列化程序11=] 对象。所以 Django REST 框架看到了这一点并抱怨,因为它无法将 LA
解析为对象。
听起来您可能真的在寻找 a SlugRelatedField
,它允许您通过 slug(在本例中为 LA
)而不是它们的主键来识别对象。
Angular/Django 休息框架
我在尝试 post 具有包含一些数组值的字段的表单数据时遇到了类似的问题。原来我在 angular 前端的 post headers 中使用了错误的 content-type。我所需要的只是注释掉 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}`;
.....
....
}
每当我们尝试将外键引用添加到实例时,Django 和 Django Rest Framework 之间存在细微差别
假设我们要将用户引用添加到另一个数据库 Table
Django
在 Django 中,我们引用用户的整个实例,如下所述
user = request.user
Django Rest 框架
但是在 DRF 中,如果我们遵循相同的过程,我们会得到类似
的错误Incorrect type. Expected pk value, received str/User type
为了克服这个错误,我们需要引用table的主键来代替(这里id是PK)
user = request.user.id