英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论