[Answered ]-KeyError: How to exclude certain parts when a part of the serializer is empty

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

Leave a comment