当更新串行化器时,嵌套的serilizer没有实例(many = true) [英] when updating serializer, nested serilizers does not have instance (many = true)

查看:483
本文介绍了当更新串行化器时,嵌套的serilizer没有实例(many = true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UserSerializer和嵌套的UserClientSerializer。我正在尝试更新日志用户的信息。但是我收到unique_together验证错误。



我有以下型号:
models.py

  class UserClients(models.Model):
user = models.ForeignKey(User,related_name ='user_clients')
client = models。 ForeignKey(Client,related_name ='client_users')
belongs_to = models.BooleanField(default = False)
for_future = models.BooleanField(default = False)

class Meta:
unique_together =('user','client')

另外我有两个seralizer。
serializers.py

  class UserClientsSerializer(serializers.ModelSerializer):

class Meta:
model = UserClients

class UserSerializer(serializers.ModelSerializer):
user_clients = UserClientsSerializer(required = False,allow_null = True,many = True)
class Meta:
model = get_user_model()
exclude =('password','username','date_joined','is_superuser')
@ transaction.atomic
def create(self,validated_data):
...
@ transaction.atomic
def update(self,instance,validated_data):
...

views.py

  class CurrentUserDetails(RetrieveUpdateAPIView):

serializer_class = UserSerializer
permission_classes =(IsAuthenticated,)

def get_object(self):
return self.request.user

所以,当我尝试更新我的用户数据时,例如belongs_to为False,我想将其更改为False。
我的JSON数据是这样的。

  {
user_clients:[
{
id:57,
belongs_to:true,
for_future:false,
user:25,
client
}
]
}

但是我收到验证错误,这个

  {
user_clients:[
{
non_field_errors:[
字段用户,客户端必须创建一个唯一的集合。
]
}
]
}

你有什么想法这个问题?

解决方案

这个链接上的第二个帖子似乎可能会描述一个类似的问题,潜在的解决方法应该适用于您:



https://github.com/tomchristie/django-rest-framework/issues/2380



好像您正在尝试创建一个新对象,而不是更新旧对象,这就是为什么你得到唯一的一起错误。



如果不是这样,那么尝试关闭验证并使用自己的如上述链接所述。


I have UserSerializer and nested UserClientSerializer. I'm trying to update info for logged user. But I receive unique_together validation error.

I have the following models: models.py

class UserClients(models.Model):
    user = models.ForeignKey(User, related_name='user_clients')
    client = models.ForeignKey(Client, related_name='client_users')
    belongs_to = models.BooleanField(default=False)
    for_future = models.BooleanField(default=False)

    class Meta:
        unique_together = ('user', 'client')

Also I have two seralizers. serializers.py

class UserClientsSerializer(serializers.ModelSerializer):

    class Meta:
        model = UserClients

class UserSerializer(serializers.ModelSerializer):
    user_clients = UserClientsSerializer(required=False, allow_null=True, many=True)
    class Meta:
        model = get_user_model()
        exclude = ('password','username', 'date_joined', 'is_superuser')
    @transaction.atomic
    def create(self, validated_data):
    ...
    @transaction.atomic
    def update(self, instance, validated_data):
    ...

views.py

class CurrentUserDetails(RetrieveUpdateAPIView):

    serializer_class = UserSerializer
    permission_classes = (IsAuthenticated,)

    def get_object(self):
        return self.request.user

So, when I trying to update my user data, for example "belongs_to" was False, I want to change it to False. My my JSON data is like this.

{
"user_clients": [
    {
        "id": 57, 
        "belongs_to": true, 
        "for_future": false, 
        "user": 25, 
        "client": 3
    }
]
}

but i receive validation error like this

{
    "user_clients": [
        {
            "non_field_errors": [
                "The fields user, client must make a unique set."
            ]
        }
    ]
}

Do you have any idea about this problem?

解决方案

The second post down on this link seems like it might describe a similar problem and a potential workaround that should work for you:

https://github.com/tomchristie/django-rest-framework/issues/2380

It seems like you are trying to create a new object rather than updating the old object which is why you are getting the unique together error.

If that is not the case then try turning off validation and using your own as described in the above link.

这篇关于当更新串行化器时,嵌套的serilizer没有实例(many = true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆