0

我在我的 DRF api 中创建了一个视图,它接收几个参数并将它们保存在数据库中。我使用从模型派生的序列化程序来执行此操作。下面是序列化程序和模型代码:

类 CreditPaymentSerializer(serializers.ModelSerializer):

session = serializers.PrimaryKeyRelatedField(
    queryset=ShoppingSession.objects.all, many=False)

class Meta:
    model = CreditPayment
    exclude = ['last_update']

模型是

class CreditPayment(models.Model):
session = models.ForeignKey('shops.ShoppingSession', on_delete=models.PROTECT)
name = models.CharField(max_length=50, default = 'none')
number = models.CharField(max_length=15, blank=False)
email  = models.CharField(max_length=40, blank=True)
amount_payed = models.IntegerField(blank=False)
total_shopping = models.IntegerField(blank=False)
amount_remaining = models.IntegerField(blank=False)
date_payment_expected = models.DateField(blank=False)
last_update = models.DateField(auto_now=True)


def __str__(self):
    return self.name

用于创建模型实例的视图集是:

class CreditPaymentView(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
queryset = CreditPayment.objects.all()
serializer_class = CreditPaymentSerializer 

def create(self, request, *args, **kwargs):
    serializer = self.get_serializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    self.perform_create(serializer)
    headers = self.get_success_headers(serializer.data)
    
    res = {
        "message": "Details Successfully created"
    }
    
    return Response(res, status=status.HTTP_201_CREATED, headers=headers)

我无法判断视图有什么问题,一旦我尝试发出发布请求,我就会收到错误消息。错误堆栈如下所示

Traceback (most recent call last):
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/sean/Development/SKAKEY/payment/views.py", line 27, in create
    serializer.is_valid(raise_exception=True)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/serializers.py", line 227, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/serializers.py", line 426, in run_validation
    value = self.to_internal_value(data)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/serializers.py", line 483, in to_internal_value
    validated_value = field.run_validation(primitive_value)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/relations.py", line 153, in run_validation
    return super().run_validation(data)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/fields.py", line 568, in run_validation
    value = self.to_internal_value(data)
  File "/home/sean/anaconda3/envs/skakey/lib/python3.8/site-packages/rest_framework/relations.py", line 264, in to_internal_value
    return queryset.get(pk=data)
AttributeError: 'function' object has no attribute 'get'
[10/Feb/2022 06:25:36] "POST /api/v1/shop/payment/credit-details/ HTTP/1.1" 500 128842

关于问题可能是什么或错误意味着什么的任何线索,还是外键关系引发了错误?

4

1 回答 1

0

更新您的序列化程序代码,如下所示:

session = serializers.PrimaryKeyRelatedField(
    queryset=ShoppingSession.objects.all(), many=False)

class Meta:
    model = CreditPayment
    exclude = ['last_update']
于 2022-02-10T06:55:38.063 回答