如何在没有 read_only=True 的情况下 Foreign_Key 值而不是 django rest 框架中的 id
How to Foreign_Key value instead of id in django rest framework without read_only=True
我在使用 drf 的项目上工作,我正在按如下方式获取序列化程序数据,这绝对没问题:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": 2,
"item": 1,
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
但我想得到如下:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": "user name",
"item": "product name",
"price: "3.44",
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
我尝试使用 drf RelatedField
和 PrimaryKryRelatedField
但在所有这些情况下,我需要将相应的字段设为 read_only=True
我想跳过。
我也试过 depth = 1
,它提供了完整的细节
我的模型:
class Cart(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
item = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField(null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return "{} - {} - {} - {} - {}".format(self.user,
self.item,
self.quantity,
self.created_at,
self.updated_at)
我的序列化器:
class CartSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
class Meta:
model = Cart
fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
我的看法:
class CartViewSet(viewsets.ModelViewSet):
queryset = Cart.objects.all().order_by('id')
serializer_class = CartSerializer
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = self.serializer_class(
queryset, context={'request': request}, many=True)
data = serializer.data
context = {
'message': 'Listed Successfully',
'count': queryset.count(),
'errors': False,
'data': data,
}
return Response(context, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs):
if not request.data:
return Response("No data", status=status.HTTP_400_BAD_REQUEST)
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
self.perform_create(serializer)
context = {
'message': 'Created Successfully',
'status': status.HTTP_201_CREATED,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(
instance, data=request.data, partial=partial)
if serializer.is_valid():
self.perform_update(serializer)
context = {
'message': 'Updated Succesfully',
'status': status.HTTP_200_OK,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
context = {
'message': 'Deleted Succesfully',
'status': status.HTTP_204_NO_CONTENT,
'errors': False,
}
return Response(context)
@action(methods=['get'], detail=False, url_path='checkout/(?P<userId>[^/.]+)', url_name='checkout')
def checkout(self, request, *args, **kwargs):
try:
user = User.objects.get(pk=int(kwargs.get('userId')))
except Exception as e:
return Response(status=status.HTTP_404_NOT_FOUND, data={'Error': str(e)})
cart_helper = CartHelper(user)
checkout_details = cart_helper.prepare_cart_for_checkout()
if not checkout_details:
return Response(status=status.HTTP_404_NOT_FOUND,
data={'error': 'Cart of user is empty.'})
return Response(status=status.HTTP_200_OK, data={'checkout_details': checkout_details})
如果有人能提供帮助,将不胜感激。
谢谢
更新您的 CartSerializer to_response 方法以动态 change/add 特定字段的值输出。像这样具有适当名称的东西应该可以解决问题
class CartSerializer(serializer.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
class Meta:
model = Cart
fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
def to_representation(self, instance):
response = super().to_representation(instance)
response['user'] = instance.user.name
response['item'] = instance.item.name
response['price'] = instance.item.price
return response
我在使用 drf 的项目上工作,我正在按如下方式获取序列化程序数据,这绝对没问题:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": 2,
"item": 1,
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
但我想得到如下:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": "user name",
"item": "product name",
"price: "3.44",
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
我尝试使用 drf RelatedField
和 PrimaryKryRelatedField
但在所有这些情况下,我需要将相应的字段设为 read_only=True
我想跳过。
我也试过 depth = 1
,它提供了完整的细节
我的模型:
class Cart(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
item = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField(null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return "{} - {} - {} - {} - {}".format(self.user,
self.item,
self.quantity,
self.created_at,
self.updated_at)
我的序列化器:
class CartSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
class Meta:
model = Cart
fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
我的看法:
class CartViewSet(viewsets.ModelViewSet):
queryset = Cart.objects.all().order_by('id')
serializer_class = CartSerializer
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = self.serializer_class(
queryset, context={'request': request}, many=True)
data = serializer.data
context = {
'message': 'Listed Successfully',
'count': queryset.count(),
'errors': False,
'data': data,
}
return Response(context, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs):
if not request.data:
return Response("No data", status=status.HTTP_400_BAD_REQUEST)
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
self.perform_create(serializer)
context = {
'message': 'Created Successfully',
'status': status.HTTP_201_CREATED,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(
instance, data=request.data, partial=partial)
if serializer.is_valid():
self.perform_update(serializer)
context = {
'message': 'Updated Succesfully',
'status': status.HTTP_200_OK,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
context = {
'message': 'Deleted Succesfully',
'status': status.HTTP_204_NO_CONTENT,
'errors': False,
}
return Response(context)
@action(methods=['get'], detail=False, url_path='checkout/(?P<userId>[^/.]+)', url_name='checkout')
def checkout(self, request, *args, **kwargs):
try:
user = User.objects.get(pk=int(kwargs.get('userId')))
except Exception as e:
return Response(status=status.HTTP_404_NOT_FOUND, data={'Error': str(e)})
cart_helper = CartHelper(user)
checkout_details = cart_helper.prepare_cart_for_checkout()
if not checkout_details:
return Response(status=status.HTTP_404_NOT_FOUND,
data={'error': 'Cart of user is empty.'})
return Response(status=status.HTTP_200_OK, data={'checkout_details': checkout_details})
如果有人能提供帮助,将不胜感激。 谢谢
更新您的 CartSerializer to_response 方法以动态 change/add 特定字段的值输出。像这样具有适当名称的东西应该可以解决问题
class CartSerializer(serializer.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
class Meta:
model = Cart
fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
def to_representation(self, instance):
response = super().to_representation(instance)
response['user'] = instance.user.name
response['item'] = instance.item.name
response['price'] = instance.item.price
return response