0

我正在使用 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?

4

1 回答 1

0

我看到你今天提出的两三个问题。我想你问错问题了。我认为您需要的是三个模型Eventactor并且repo。该event模型有两个外键字段,分别为actorrepo。现在您希望它更新actor模型的 avtar_url 字段。好的?

class Actor(models.Model):
        avtar_url = models.CharField(max_length=255)
       # Other Fields

class Repo(models.Model):
       # Other Fields

class EventModel(models.Model):
        id = models.CharField(primary_key=True, max_length=255)
        type = models.CharField(max_length=255)
        actor = models.ForaignKey(Actor)
        repo = models.ForaignKey(Actor)
        created_at = models.DateTimeField()

现在使用writable-nested-serializers创建和更新NESTED EventModel条目。这样,您可以通过其 id直接更新for 。avatar_urlActor

根据请求更新

您需要按以下方式更改您的方法,create以便它创建并将它们的 id 链接到ActorRepoEvent

def create(self, validated_data):
        actor = validated_data.pop('actor')
        repo = validated_data.pop('repo')
        actor = Actor.objects.create(**actor)
        repo = Repo.objects.create(**repo)
        return Event.objects.create(actor=actor,repo=repo,**validated_data)
于 2019-02-18T14:43:33.480 回答