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?.
Aucun commentaire:
Enregistrer un commentaire