DRF报错‘str’ object has no attribute ‘pk’ 技术求助
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=Trueflag 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
ModelSerializertied 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
Serializerinstead ofModelSerializer. This avoids the implicitpkrequirement 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 throughserializer.validated_data(which will be a list of dictionaries) to process each switch request. - If you removed
many=True, accessserializer.validated_datadirectly 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
manyflag (array =many=True, single object =many=False). - Use a regular
Serializerif 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

