Django 休息框架 - "This field is required."
Django rest framework - "This field is required."
我有两个 classes,class 金额和交易
class Account(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
account_number = models.BigIntegerField(unique=True)
currency = models.CharField(max_length=3)
balance = models.DecimalField(max_digits=20, decimal_places=2, default=0)
class Transaction(models.Model):
sender_account_number = models.BigIntegerField()
recipient_account_number = models.BigIntegerField()
amount = models.DecimalField(max_digits=20, decimal_places=2)
date_creation = models.DateTimeField(auto_now_add=True)
account = models.ForeignKey(Account, on_delete=models.CASCADE)
我的序列化程序:
class TransactionSerializer(serializers.ModelSerializer):
class Meta:
model = Transaction
fields = ['sender_account_number', 'recipient_account_number', 'amount', 'date_creation', 'account']
extra_kwargs = {
'date_creation': {'read_only': True},
}``
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['id', 'account_number', 'currency', 'balance', 'user']
extra_kwargs = {
'balance': {'read_only': True},
'user': {'read_only': True},
}
我想向数据库添加一个事务,但它会根据帐号自动设置我的 account_id,因为一个用户可以有多个帐户*。
我尝试了 perform_create 方法
def perform_create(self, serializer):
serializer.save(account=Account.objects.values('id').filter(
account_number=self.request.data['sender_account_number']))
但是我失败了。我收到错误
{
"account": [
"This field is required."
]
}
比如我要发送这些数据
{
"sender_account_number": 346,
"recipient_account_number": 546,
"amount": 60
}
和Post方法:
def post(self, request):
serializer = TransactionSerializer(data=request.data)
serializer.is_valid(raise_exception=True):
return Response(serializer.data)
如何解决?
您必须告诉您的事务序列化程序该帐户是一个外键。因此,它需要一个对象而不是字符串。
添加 account = serializers.StringRelatedField() 像这样
class TransactionSerializer(serializers.ModelSerializer):
account = serializers.StringRelatedField()
class Meta:
model = Transaction
fields = ['sender_account_number', 'recipient_account_number', 'amount', 'date_creation', 'account']
extra_kwargs = {
'date_creation': {'read_only': True},
}``
如果这样做,则不需要执行创建功能。序列化器应该自动为你添加它。您只需要从您的帐户模型中检索带有您帐号的实例。然后在检查序列化程序是否有效后将其传递给 serializer.save() 。像这样
def post(self, request):
account_number = request.data.get('sender_account_number')
account = Account.objects.get(account_number=account)
serializer = TransactionSerializer(data=request.data)
serializer.is_valid(raise_exception=True):
serializer.save(account = account_number)
return Response(serializer.data)
我有两个 classes,class 金额和交易
class Account(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
account_number = models.BigIntegerField(unique=True)
currency = models.CharField(max_length=3)
balance = models.DecimalField(max_digits=20, decimal_places=2, default=0)
class Transaction(models.Model):
sender_account_number = models.BigIntegerField()
recipient_account_number = models.BigIntegerField()
amount = models.DecimalField(max_digits=20, decimal_places=2)
date_creation = models.DateTimeField(auto_now_add=True)
account = models.ForeignKey(Account, on_delete=models.CASCADE)
我的序列化程序:
class TransactionSerializer(serializers.ModelSerializer):
class Meta:
model = Transaction
fields = ['sender_account_number', 'recipient_account_number', 'amount', 'date_creation', 'account']
extra_kwargs = {
'date_creation': {'read_only': True},
}``
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['id', 'account_number', 'currency', 'balance', 'user']
extra_kwargs = {
'balance': {'read_only': True},
'user': {'read_only': True},
}
我想向数据库添加一个事务,但它会根据帐号自动设置我的 account_id,因为一个用户可以有多个帐户*。 我尝试了 perform_create 方法
def perform_create(self, serializer):
serializer.save(account=Account.objects.values('id').filter(
account_number=self.request.data['sender_account_number']))
但是我失败了。我收到错误
{
"account": [
"This field is required."
]
}
比如我要发送这些数据
{
"sender_account_number": 346,
"recipient_account_number": 546,
"amount": 60
}
和Post方法:
def post(self, request):
serializer = TransactionSerializer(data=request.data)
serializer.is_valid(raise_exception=True):
return Response(serializer.data)
如何解决?
您必须告诉您的事务序列化程序该帐户是一个外键。因此,它需要一个对象而不是字符串。 添加 account = serializers.StringRelatedField() 像这样
class TransactionSerializer(serializers.ModelSerializer):
account = serializers.StringRelatedField()
class Meta:
model = Transaction
fields = ['sender_account_number', 'recipient_account_number', 'amount', 'date_creation', 'account']
extra_kwargs = {
'date_creation': {'read_only': True},
}``
如果这样做,则不需要执行创建功能。序列化器应该自动为你添加它。您只需要从您的帐户模型中检索带有您帐号的实例。然后在检查序列化程序是否有效后将其传递给 serializer.save() 。像这样
def post(self, request):
account_number = request.data.get('sender_account_number')
account = Account.objects.get(account_number=account)
serializer = TransactionSerializer(data=request.data)
serializer.is_valid(raise_exception=True):
serializer.save(account = account_number)
return Response(serializer.data)