我正在使用 Python(3)、Django(1.11) 和 DRF(3.6) 开发一个项目,其中我必须PUT
通过传递 anested nested
而不是ID
.
这是我尝试过的:
models.py
:
class Actor(models.Model):
id = models.CharField(primary_key=True, max_length=255)
login = models.CharField(max_length=255)
avatar_url = models.URLField(max_length=500)
class Repo(models.Model):
id = models.CharField(primary_key=True, max_length=255)
name = models.CharField(max_length=255)
url = models.URLField(max_length=500)
class Event(models.Model):
id = models.CharField(primary_key=True, max_length=255)
type = models.CharField(max_length=255)
actor = models.ForeignKey(Actor, related_name='actor')
repo = models.ForeignKey(Repo, related_name='repo')
created_at = models.DateTimeField()
serializers.py
:
class ActorSerializer(serializers.ModelSerializer):
class Meta:
model = Actor
fields = ('id', 'login', 'avatar_url')
class RepoSerializer(serializers.ModelSerializer):
class Meta:
model = Repo
fields = ('id', 'name', 'url')
class EventModelSerializer(serializers.ModelSerializer):
actor = ActorSerializer(many=False)
repo = RepoSerializer(many=False)
class Meta:
model = Event
fields = ('id', 'type', 'actor', 'repo', 'created_at')
depth = 1
def create(self, validated_data):
return Event.objects.create(**validated_data)
更新:当我提交带有以下对象的发布请求时:
{
"id":ID,
"type":"PushEvent",
"actor":{
"id":ID,
"login":"daniel33",
"avatar_url":"https://avatars.com/2790311"
},
"repo":{
"id":ID,
"name":"johnbolton/exercitationem",
"url":"https://github.com/johnbolton/exercitationem"
},
"created_at":"2015-10-03 06:13:31"
}
它将此错误返回为:
TypeError: 'ValueError: Cannot assign "OrderedDict([('id', '2790311'), ('login', 'daniel33'), ('avatar_url', 'https://avatars.com/2790311')])": "Event.actor" must be a "Actor" instance.
views.py
:
class Actor(generics.GenericAPIView):
serializer_class = EventModelSerializer
queryset = EventModel.objects.all()
def update(self):
actor = EventModel.objects.filter(actor_id=self.request.data('id'))
print(actor)
return HttpResponse(actor)
Sample Input Object
:
{
"id":3648056,
"login":"ysims",
"avatar_url":"https://avatars.com/modified2"
}
要求是:
更新 avatar URLactor
:服务应该能够通过 PUT 请求更新 actor 的 avatar URL /actors
。演员 JSON 在请求正文中发送。如果具有 id 的参与者不存在,则响应代码应该是404
,或者如果有其他字段正在为参与者更新,那么 HTTP 响应代码应该是400
,否则,响应代码应该是200
。**
我对如何在
PUT
不通过ID
?