Serializer不会将数据保存到数据库,但响应状态码为200。

huangapple go评论72阅读模式
英文:

Serializer doesn't save data into DB, but response is 200

问题

I need to take data by using keys from different models, and then put them together and save into another model.

views.py:

    def create_order(self, request):
        instance = self.get_object()
        serializer = CreateOrderSerializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        print(serializer.data)
        return Response(serializer.data)

serializers.py:

class CreateOrderSerializer(serializers.ModelSerializer):
    user_name = serializers.ReadOnlyField(source="author.username", required=False, allow_null=True)
    book = serializers.ReadOnlyField(source="book_name", required=False, allow_null=True)

    class Meta:
        model = order
        fields = ("id", "user_name", "book")

I can access and operate with data which I need, and I get a 200 response when making a POST request, but data doesn't save. By the way, when I create an "order" in the admin menu everything works fine, thus I assume something is going wrong in either my view or serializer.

英文:

I need to take data by using keys from different models, and then put them together and save into another model.

views.py:

    def create_order(self, request):
        instance = self.get_object()
        serializer = CreateOrderSerializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        print(serializer.data)
        return Response(serializer.data)

serializers.py:

class CreateOrderSerializer(serializers.ModelSerializer):
    user_name = serializers.ReadOnlyField(source="author.username", required=False, allow_null=True)
    book = serializers.ReadOnlyField(source="book_name", required=False, allow_null=True)

    class Meta:
        model = order
        fields = ("id", "user_name", "book")

I can access and operate with data which i need, and i get 200 response when make POST request, but data doesn't save. By the way, when i create "order" in admin menu everythink works fine, thus i assume something is going wrong in either my view or serializer.

答案1

得分: 0

serializer.py

class CreateOrderSerializer(serializers.ModelSerializer):
    user_name = serializers.CharField(source="author.username", required=False, allow_null=True)
    book = serializers.CharField(source="book_name", required=False, allow_null=True)

    class Meta:
        model = order
        fields = ("id", "user_name", "book")

views.py

def create_order(self, request):
    instance = self.get_object()
    serializer = CreateOrderSerializer(instance, data=request.data)
    serializer.is_valid(raise_exception=True)
    user_id = request.user
    order.objects.create(
        user=user_id,
        book=instance,
    )
    return Response(serializer.data)
英文:

Finally i fixed this.

serializer.py

class CreateOrderSerializer(serializers.ModelSerializer):
    user_name = serializers.CharField(source="author.username", required=False, allow_null=True)
    book = serializers.CharField(source="book_name", required=False, allow_null=True)

    class Meta:
        model = order
        fields = ("id", "user_name", "book")

views.py

    def create_order(self, request):
        instance = self.get_object()
        serializer = CreateOrderSerializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        user_id = request.user
        order.objects.create(
            user=user_id,
            book=instance,
        )
        return Response(serializer.data)

huangapple
  • 本文由 发表于 2023年5月14日 00:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定