1
シリアライザを作成しましたが、POSTデータを検証した後、シリアライザの予約フィールドからBooking
インスタンスを作成しようとしています。しかし、Booking
オブジェクトが、私はエラーを取得しています、外部キーを持っているので:ValueError: Cannot assign "4": "Booking.activity" must be a "Activity" instance.
Django Rest Framework - シリアル化後のモデルインスタンスの取得
ここに私のビュー機能があります:BookingSerializer
はModelSerializer
class ChargeCustomerRequestSerializer(serializers.Serializer):
booking = BookingSerializer()
customer = serializers.CharField(max_length=255)
class BookingSerializer(serializers.ModelSerializer):
class Meta:
model = Booking
fields = '__all__'
# I wanted to view the instances with the nested information available
# but this breaks the serializer validation if it's just given a foreign key
# depth = 1
何ですか
@api_view(['POST'])
def customer_charge(request):
serializer = ChargeCustomerRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
# trying to create an instance using the ReturnDict from the serializer
booking = Booking(**serializer.data['booking'])
booking.save()
Serializers.pyネストされたシリアライザからモデルインスタンスを作成する正しい方法はありますか?