gpt4 book ai didi

python - 使用 Django Rest Framework 序列化器创建外键关系的方法

转载 作者:行者123 更新时间:2023-12-04 00:03:49 25 4
gpt4 key购买 nike

我的模型是这样的:

class FirewallPolicy(models.Model):
name = models.CharField(max_length=100, unique=True)
team = models.ForeignKey(Team)
source_ip = models.ForeignKey(IP)
destination_ip = models.ForeignKey(IP)

现在,为了创建新的防火墙策略,应该已经有一个现有团队、source_ip 和 destination_ip。我创建新防火墙策略的负载如下:
{"name": "test-create-policy-911",
"team": "avengers",
"source_ip": "1.1.1.1",
"destination_ip": "2.2.2.2",
}

我创建新防火墙策略的序列化程序如下:
class FirewallPolicyCreateSerializer(serializers.ModelSerializer):
name = serializers.CharField(max_length=100)
team = serializers.CharField(max_length=100)
source_ip = serializers.CharField(max_length=100)
destination_ip = serializers.CharField(max_length=100)

class Meta:
model = Policy
fields = ['id', 'name', 'team', 'source_ip', 'destination_ip']

def validate(self, data):
try:
Team.objects.get(name=data['team'])
IP.objects.get(name=data['source_ip'])
IP.objects.get(name=data['destination_ip'])
except ObjectDoesNotExist:
raise serializers.ValidationError("Entities must exist before you can associate it with a Firewall Policy")

def create(self, validated_data):
team = Team.objects.get(name=validated_data['team'])
source_ip = IP.objects.get(name=validated_data['source_ip'])
destination_ip = IP.objects.get(name=validated_data['destination_ip'])
policy = Policy.objects.create(name=validated_data['name'],
team_id=team.id,
source_ip_id = source_ip.id,
destination_ip_id = destination_ip.id )
return policy

我不确定这是否是向模型添加外键的正确方法,因为它看起来工作量太大。在序列化程序可以自动检查所有这些并添加外键的地方,我是否遗漏了什么?

最佳答案

是的,你做的工作比你需要的多得多。

您应该使用 SlugRelatedField 定义您的字段允许 DRF 从相关模型上的字段自动填充它们。所以:

class FirewallPolicyCreateSerializer(serializers.ModelSerializer):
team = serializers.SlugRelatedField(queryset=Team.objects.all(), slug_field='name')
source_ip = serializers.SlugRelatedField(queryset=IP.objects.all(), slug_field='source_ip')
destination_ip = serializers.SlugRelatedField(queryset=IP.objects.all(), slug_field='destination_ip')

class Meta:
model = Policy
fields = ['id', 'name', 'team', 'source_ip', 'destination_ip']

现在你不需要定义 validatecreate根本没有,因为 DRF 将完成所有相关的验证和分配。

(注意,您也不需要重新定义 name 字段,因为您不会更改底层模型字段的任何内容。)

关于python - 使用 Django Rest Framework 序列化器创建外键关系的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53251757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com