ring_22
ring_22

Reputation: 77

'dict' object has no attribute 'pk' Django Rest API Framework

Im using Django Rest API Framework, I want to upload multiple images for a single project using Angular js.

Here's my model:

class Project(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    owner = models.ForeignKey(User)
    number_of_photos = models.IntegerField()

class Photo(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    images = models.ImageField(upload_to='photos/', max_length=254)
    project = models.ForeignKey(Project)

I have this serializers:

class ProjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Project
        fields = ('id', 'created', 'number_of_photos', 'owner')

    def create(self, validated_data):
        project = Project.objects.create(**validated_data)
        return project

class UploadSerializer(serializers.ModelSerializer):
    project = ProjectSerializer(many=True, read_only=True)

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

In my view I got this inside my viewsets.ModelViewSet

serializer = UploadSerializer(data=photo_array, many=True, context={'request': request})

if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

The variable photo_array contains:

[{'project': u'1', 'images': {u'name': u'test-image.png', u'lastModifiedDate': u'2015-04-22T08:51:11.000Z', u'webkitRelativePath': u'', u'lastModified': 1429692671000, u'type': u'image/png', u'size': 43152}}, {'project': u'1', 'images': {u'name': u'test.png', u'lastModifiedDate': u'2015-04-08T08:35:17.000Z', u'webkitRelativePath': u'', u'lastModified': 1428482117000, u'type': u'image/png', u'size': 127433}}]

But it gives me an error 'dict' object has no attribute 'pk'

Did my photo_array variable cause this problem?.

Upvotes: 0

Views: 7248

Answers (2)

Kevin Brown-Silva
Kevin Brown-Silva

Reputation: 41719

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.

Upvotes: 1

partha
partha

Reputation: 1

I think you should update your UploadSerializer with id field

Upvotes: 0

Related Questions