[Answer]-'dict' object has no attribute 'pk' Django Rest API Framework

1👍

Without a traceback, I can only take an educated guess as to what the issue actually is.

You are using a standard ModelSerializer and you are allowing Django REST framework to generate your fields for you. You can introspect the generated fields by printing the output of repr(UploadSerializer()), but they should look something like this:

class UploadSerializer(ModelSerializer):
    url = HyperlinkedIdentityField()
    created = DateTimeField()
    images = ImageField()
    project = ProjectSerializer(many=True, read_only=True)

    class Meta:
        model = Photo
        fields = ('url', 'created', 'images', 'project')

With those fields, a typical dictionary that would be passed back from the serializer should look something like

{
    "id": 1,
    "url": "http://localhost:8000/api/photos/1/",
    "created": "2015-04-22T08:51:11.000Z",
    "images": "http://localhost:8000/media/test-image.png",
    "project": {
        "id": 1,
        "created": "2015-04-22T08:51:11.000Z",
        "number_of_photos": 1,
        "owner": 1
    }
}

You’ll notice that this is completely different from what you are passing in. You are passing in the data that would match a serializer that looks like

class UploadSerializer(ModelSerializer):
    url = HyperlinkedIdentityField()
    created = DateTimeField()
    images = SomeCustomImageField()
    project = PrimaryKeyRelatedField()

    class Meta:
        model = Photo
        fields = ('url', 'created', 'images', 'project')

So that does answer your secondary question

Did my photo_array variable cause this problem?

Most likely. Now, I don’t actually know what your issue is. It sounds like you are passing a dictionary into a PrimaryKeyRelatedField, but your serializers don’t actually match up.

0👍

I think you should update your UploadSerializer with id field

👤partha

Leave a comment