mongodb Django EmbeddedModelField在执行PUT请求时说“This field may not be blank”,尽管字段具有“blank=True”

6qqygrtg  于 2023-04-05  发布在  Go
关注(0)|答案(3)|浏览(356)

我正在使用django-rest-framework创建一个Django应用程序,并使用djongo连接到MongoDB。我有这样的嵌套模型:

class Group(models.Model):
    users = models.ArrayModelField(
        model_container=User
    )

class User(models.Model):
    number = models.IntegerField(
        default=None,
        null=True
    )

    song = models.EmbeddedModelField(
        model_container=Song,
        null=True,
        blank=True
    )

    class Meta:
        abstract = True

class Song(models.Model):
    mp3_file = models.URLField(
        default=None,
        blank=True,
        null=True
    )

    comments = models.CharField(
        max_length=250,
        default='',
        blank=True
    )

    class Meta:
        abstract = True

创建组时,用户和歌曲的创建没有任何问题。例如,创建组时,组可能看起来像:

{
    "name": "Sample",
    "users: [
        {
            "number": null,
            "song": {
                "mp3_file": null,
                "comments": ""
            }
        }
    ]
}

这一切都很好。但是,如果我尝试执行PUT请求,并且不更改“number”,“mp3_file”或“comments”的值,我将得到以下错误消息:

"user": [
    {
        "number": [
            "This field may not be null."
        ],
        "song": {
            "mp3_file": [
                "This field may not be null."
            ],
            "comments": [
                "This field may not be blank."
            ]
        }
    }
]

有什么关于如何修复这个错误的想法吗?我只是使用一个通用的RetrieveUpdateDestroyAPIView作为这个端点的视图。
edit:我还尝试过重新创建所有迁移以及删除表并重新创建它,但这两种方法都没有帮助。
edit:这里是视图:

class GroupDetail(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = GroupSerializer
    queryset = Group.objects.all()
    lookup_field = 'group_name'
    lookup_url_kwarg = 'group_name'

序列化器:

class GroupSerializer(serializers.HyperlinkedModelSerializer):
    users = UserSerializer(many=True)

    def update(self, instance, validated_data):
        if validated_data.get('group_name', None) is not None:
            instance.__setattr__('group_name', validated_data.get('group_name'))

        instance.save()

        return instance

    class Meta:
        model = Group
        fields = (
            'group_name',
            'users'
        )

class UserSerializer(serializers.Serializer):
    number = serializers.IntegerField()
    song = SongSerializer()

class SongSerializer(serializers.Serializer):
    mp3_file = serializers.URLField()
    comments = serializers.CharField(
        max_length=250
    )

但问题的一部分是,由于数据验证不正确,因此甚至永远不会到达序列化程序。

rekjcdws

rekjcdws1#

尝试改变:

class UserSerializer(serializers.Serializer):
    number = serializers.IntegerField(allow_null=True)
    song = SongSerializer()

class SongSerializer(serializers.Serializer):
    mp3_file = serializers.URLField(allow_blank=True)
    comments = serializers.CharField(
        max_length=250, allow_blank=True
    )
ca1c2owp

ca1c2owp2#

尝试rest_meets_djongo,并将serializers.Serializer替换为DjongoModelSerializer

wgmfuz8q

wgmfuz8q3#

通过在CharField中同时设置allow_null=True和allow_blank=True,解决了这个问题!

text = serializers.CharField(
      required=False, allow_null=True, allow_blank=True
)

相关问题