mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 17:08:41 -06:00
refactor to handle M2M validation in ValidatedModelSerializer
This commit is contained in:
parent
0357d8522c
commit
b34f4f8e43
@ -734,30 +734,13 @@ class WritableInterfaceSerializer(ValidatedModelSerializer):
|
|||||||
# Validate that all untagged VLANs either belong to the same site as the Interface's parent Deivce or
|
# Validate that all untagged VLANs either belong to the same site as the Interface's parent Deivce or
|
||||||
# VirtualMachine, or are global.
|
# VirtualMachine, or are global.
|
||||||
parent = self.instance.parent if self.instance else data.get('device') or data.get('virtual_machine')
|
parent = self.instance.parent if self.instance else data.get('device') or data.get('virtual_machine')
|
||||||
tagged_vlans = data.pop('tagged_vlans', [])
|
for vlan in data.get('tagged_vlans', []):
|
||||||
for vlan in tagged_vlans:
|
|
||||||
if vlan.site not in [parent, None]:
|
if vlan.site not in [parent, None]:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
"Tagged VLAN {} must belong to the same site as the interface's parent device/VM, or it must be "
|
"Tagged VLAN {} must belong to the same site as the interface's parent device/VM, or it must be "
|
||||||
"global".format(vlan)
|
"global".format(vlan)
|
||||||
)
|
)
|
||||||
|
return data
|
||||||
validated_data = super(WritableInterfaceSerializer, self).validate(data)
|
|
||||||
if tagged_vlans:
|
|
||||||
validated_data['tagged_vlans'] = tagged_vlans
|
|
||||||
return validated_data
|
|
||||||
|
|
||||||
def create(self, validated_data):
|
|
||||||
"""
|
|
||||||
Becasue tagged_vlans is a M2M relationship, we have to create the interface first
|
|
||||||
"""
|
|
||||||
tagged_vlans = validated_data.pop('tagged_vlans', None)
|
|
||||||
interface = Interface.objects.create(**validated_data)
|
|
||||||
interface.save()
|
|
||||||
if tagged_vlans:
|
|
||||||
interface.tagged_vlans = tagged_vlans
|
|
||||||
interface.save()
|
|
||||||
return interface
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user