You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DRF报错‘str’ object has no attribute ‘pk’ 技术求助

Fixing the 'str' object has no attribute 'pk' Error in Your Radio Stream Switching Feature

Hey there, let's break down that frustrating error you're seeing. I've run into similar serializer mishaps before, so here's what's probably going wrong and how to fix it.

Why This Error Happens

The 'str' object has no attribute 'pk' error almost always means your serializer is expecting to work with model instances (which have a pk attribute) or properly structured dictionaries, but it's getting raw strings instead. This is super common when there's a mismatch between the JSON data you're sending and how you've configured the serializer (especially with the many=True flag).

Step-by-Step Fixes

1. Check Your Request JSON Structure

First, make sure the JSON your frontend sends matches whether you're using many=True or not:

  • If you're using many=True, your payload must be an array of objects, like this:
    [
      {"input_source": "mic1", "schedule_time": "2024-05-20T14:30:00"},
      {"input_source": "line_in", "schedule_time": null}
    ]
    
  • If you're sending a single switch request, your payload should be a single object (not an array), and you should remove the many=True flag from your serializer call.

If you pass a single object to a serializer with many=True, it'll iterate over the object's keys (which are strings like "input_source"), and those strings don't have a pk attribute—hence the error.

2. Validate Your Serializer Setup

Let's look at your RadioSwitchSerializer:

  • If you're using a ModelSerializer tied to a database model, it expects data that maps to model fields. If your request includes a field that's supposed to link to another model (e.g., a foreign key), make sure you're passing the primary key value (not a string name of the related object).
  • If you don't actually need to create/update database records—you just want to validate the request data to trigger the switch—use a regular Serializer instead of ModelSerializer. This avoids the implicit pk requirement tied to model instances.

Example of a simplified validation-only serializer:

# serializers.py
from rest_framework import serializers

class RadioSwitchSerializer(serializers.Serializer):
    input_source = serializers.CharField(max_length=50)
    schedule_time = serializers.DateTimeField(required=False, allow_null=True)

3. Adjust Your View Logic

In your views.py, double-check how you're handling the serializer data:

  • If you're using many=True, loop through serializer.validated_data (which will be a list of dictionaries) to process each switch request.
  • If you removed many=True, access serializer.validated_data directly as a single dictionary.

Avoid trying to treat raw request data as model instances—always use the validated data from the serializer to ensure you're working with the right data types.

Quick Recap

  • Match your JSON payload structure to the many flag (array = many=True, single object = many=False).
  • Use a regular Serializer if you're just validating data, not interacting with a database model.
  • Ensure foreign key fields (if any) receive primary key values, not string labels.

That should get rid of the pk error and let your stream switching feature work as intended!

内容的提问来源于stack exchange,提问作者Magrias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:14:18