1👍
✅
You can add default value in pop function, so it wont raise keyerror
def create(self, validated_data):
assigned_facilities = validated_data.pop("assigned_facilities", [])
instance = Lead.objects.create(**validated_data)
for item in assigned_facilities:
instance.leadfacility.create(**item)
else:
print("No Facilities Added!")
return instance
Source:stackexchange.com