使用Django Rest API框架,我想使用Angular js上傳單個項目的多個圖像。'dict'對象沒有屬性'pk'Django Rest API框架
這裏是我的模型:
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)
我有這樣的序列化:
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')
在我看來,我得到這個我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)
變量photo_array內包含:
[{'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}}]
但它給我一個錯誤'字典' 對象有沒有屬性 'PK'
我的photo_array變量原因這個問題?
如果您在帖子中包含錯誤追蹤,那將會非常有用。 –